function in Haskell that can solve an equation [closed] - function

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need to write a function in Haskell that can solve the following problem(physic_problem):
What is the height (in a whole number of meters) of the shortest building that you could drop a ball from such that it would take at least 5 seconds to hit the ground?
The equation can be found here http://en.wikipedia.org/wiki/Equations_for_a_falling_body
I really tried hard on this and i need help!
Thank you so much!

Consider for instance this,
g :: Double
g = 9.81
dist :: Double -> Double
dist t = g * t^2 / 2
Then,
> dist 5
122.625
Additionally you may want to create a module out of the equations in that Wiki.
Update
For delivering an integral value consider for example
dist' :: (Integral a) => Double -> a
dist' t = ceiling $ g * t^2 / 2
Here we use ceiling (upper bound), yet note Converting to Integral for other rounding functions. Hence,
> dist' 5
123

Related

Octave - How to plot an "infinite"(= Defining the function on [0:35916] for me) sawtooth function [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I know how to plot a sawtooth function (thanks to another forum) but only on the domain [0:10] thanks to the following code which is actually working :
t=0:0.04:10;
A=1;
T=1;
rho= mod(t * A / T, A);
plot(t,rho)
A=the amplitude, T=the period,t=the time interval.
The problem is that I need the same function on the domain [0:35916] but when I try to adapt this code to do so (eg by extending the time interval), I get an error and I don't understand why.
error:
plt2vv: vector lengths must match error: called from plt>plt2vv at line 487 column 5 plt>plt2 at line 246 column 14 plt at line 113 column 17 plot at line 222 column 10
Simply modifying the original upper limit of your interval from 10 to 35916 should do the trick:
t=0:0.04:35916;
A=1;
T=1;
rho= mod(t * A / T, A);
plot(t,rho)
The code above yields the following image:
Of course it is up to you to adjust A and T to suit your needs.

How do I convert 234.35 into Binary? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I need to convert the above into Binary. I'm at a loss here.
Can you include full information on how you got the answer too?.
Step by step would be great!
First, convert the integral part which is 11101010. Next, you build the decimal part. If demonstrated informally, it is something like:
0.1 is one half, but 0.35 is less than that, so the first binary digit is zero -> .0 (0.35)
0.01 is one quarter, and 0.35 is greater than that, so then follows one. 0.35 minus one quarter is decimal 0.1 -> .01 (0.35 - 0.25 = 0.1)
0.001 is one eighth, and decimal 0.1 is less than that, so again zero -> .010 (0.1)
Next steps are: .0101 (0.1 - 0.0625 = 0.0375); .01011 (0.0375 - 0.03125 = 0.00625); then follow two zeroes, and we can probably cut it here.
So, the answer is 11101010.010110... As the original number has decimal part of 7/20, it becomes infinite when converted to binary.

Haskell define a function [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have to make a function like this:
success :: (Int,Int,Int) -> Int -> (Int,Int,Int) -> Bool
(Int,Int,Int) First three Ints are attributes (that means numbers from 1 to 20)
Int in the middle is the one to compare the differences between attributes and dices.
(Int,Int,Int) the last three Ints are diced by a dice with 20 sides for each of them.
sucess (attribute 1,attribute 2, attribute 3) -> Compare difference between attributes and dices -> (Dice 1,Dice 2,Dice 3) -> Bool
Now I have to compare attribute 1 and dice 1 and so on.
If the dice is higher then the attribute, I've to notice the difference.
If lower I've to notice 0. If the three differences are higher than the difficult I lose
=> function is false
For example:
success (16,13,8) 4 (1,17,10) => false difference is 6
success (16,13,8) 4 (1,10, 9) => true difference is only 1!
I am a newby at haskell-programming and have no idea how to handle this.
This looks like a homework assigment, you're supposed to do it by yourself. So I will offer some hints instead of the full working solution.
You could define that function by using pattern matching:
success (a1, a2, a3) d (d1, d2, d3) = ... -- put condition at here
where diff1 = ...
diff2 = ...
diff3 = ...
I believe you can fill in the missing details quite easily.

Bouncing off inclined surface [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I can calculate distance between inclined line and my ball (with normal vector), But how can I calculate new velocity?
Anders' answer was a good one but I realise that you may not have a great mathematical back ground so I will elaborate. The problem you have at the moment is poorly stated. However, see the following figure
This will allow us to derive the equation you require. Now, the scalar product of two vectors a and b, a.b gives the magnitude of a multiplied by the projection of b onto a. Basically, if we take n as a unit vector (magnitude 1 in each component direction) then a.n gives the magnitude of the components of a which act in the direction of n.
So, splitting the velocity components into those parallel and perpendicular to the plain; to get the velocity V we first split U into components.
Perpendicular to the plane in direction n, we have a vector velocity w = (U.n) n. This means that in fact we can write U = (U.n) n + [U - (U.n) n]. This is saying that U is made up of the perpendicular component of itself + the parallel component of itself. Now, -V is very similar to U but the parallel components acts in the reverse direction, so we can write -V = (U.n) n - [U - (U.n) n].
Combining the above gives the result Anders stated, i.e. V = U -2[(U.n) n]. The dot/scalar product is defined as a.b = |a||b|cos(A) where A is the angle between the vectors laid together tail-to-tail, this should enable you to solve your problem.
I hope this helps
If The vector v=(vx,vy) is the initial velocity and the plane has normal n=(nx,ny) then the new reflected velocity vector r will be
r=v−2(v⋅n)*n
The product (v⋅n) is the dot product of v and n, defined as vxnx+vyny. Note that the plane normal must be normalized (length 1.0). A related question with the same answer https://math.stackexchange.com/questions/13261/how-to-get-a-reflection-vector

Vectorization or sum as matrix operations [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
Let there be the following definition of gradient descent cost function
with the hypothesis function defined as
what I've come up with for multivariate linear regression is
theta = theta - alpha * 1/m * ([theta', -1]*[X';y']*X)';
h_theta = 1/(2*m)* (X*theta - y)'*(X*theta-y);
(octave notation, ' means matrix transpose, [A, n] means adding a new column to matrix A with scalar value n, [A; B] means appending matrix B to matrix A row-wise)
It's doing its job correctly how far I can tell (the plots look ok), however I have a strong feeling that it's unnecessarily complicated.
How to write it with as little matrix operations as possible (and no element-wise operations, of course)?
I don't think that is unnecessarily complicated, and instead this is what you want. Matrix operations are good because you don't have to loop over elements yourself or do element-wise operations. I remember taking a course online and my solution seems pretty similar.
The way you have it is the most efficient way of doing it as it is fully vectorized. It can be done by having a for loop over the summation and so on, however this is very inefficient in terms of processing power.