I trying to implement the below answer by Yves Daoust.
https://math.stackexchange.com/questions/1065013/rotation-to-obtain-corodinate
Code
to-report calx2 [x0 y0 x1 y1 A]
report x0 + (y1 - y0) * tan ( A + atan (y1 - y0) (x1 - x0))
end
I don't seem to get intended output(can't figure the mistake from o/p testing). Where am going wrong? It seems I have some problem in how to implementing atan in NetLogo considering the axes are according to turtle.
Please point the error if possible.
Also, are there any easier ways to accomplish the same in NetLogo.
I get the mistake it should atan x y. As I was inverse it two times as the answered question already considered the angle A with vertical axis ,that is, NetLogo's axis style.
Related
In my problem the linear modal is defined as the first term in the series expansion of sin(x) so:
sin(x) = x is the linear modal.
So with this, I then have to write
as a system of x' and y', so:
I tried the linear modal in Euler's method, with initial conditions X(1) = 1 and V(1)=0 (V would be y then following the last equations):
for i = 1:1000
V(i+1) = V(i)-(1.*s) ;
X(i+1) = V(i);
end
Where s is the step size. But apparently I'm supposed to get a circle when I plot V with respect to X which makes sense, but all I get is a straight line.
If I change it to:
for i = 1:1000
V(i+1) = V(i)-(X(i).*s) ;
X(i+1) = V(i);
end
With s=0.8 I get a spiral, which looks like a development but I'm no closer to the circular shape that I am expecting. I think I just need a fresh pair of eyes to see where perhaps an obvious error lies.
You are coding something drastically different than you wrote down in the equations. If you wrote down the equations with the same order and variable names, you would probably already see your error.
Also, the following things should have tipped you off:
The step size does not appear as a linear factor in both equations.
In your first attempt, X has no impact on V.
Nonetheless, here is a direct translation of your equations to code (and the Euler method):
for i = 1:1000
x(i+1) = x(i) + y(i)*s;
y(i+1) = y(i) + sin(x(i))*s;
end
If you really want to use the linear approximation, just replace sin(x(i)) by x(i). However, the main point of the linear approximation is that it’s nice for analytical solutions and theoretical analysis. Since you are solving the problem numerically, I do not see a need for it.
Finally, note that since you are integrating a Hamiltonian system, you will very quickly see the problems of the Euler method, i.e., you will always get a blatant spiral. Just imagine what would happen if you simulate the motion of the earth around the sun with tangential vectors:
I am using contourf to generate a contour plot for a 2 variable function.
My function is Z = f(x,y).
I generate x and y through meshgrid function in matlab and generate values for Z and then plot the contour using contour(x,y,z).
I want to be able to calculate the volume under this generated contour. Can anyone please help ?
Thanks in advance
couldn't you simply use a integral approximation like a riemann sum? Assuming uniform spacing for x and y something like this should work
delta_x = x(2) - x(1);
delta_y = y(2) - y(1);
vol = sum(Z(:)) * delta_x * delta_y;
This will not be the EXACT volume, but an approximation. Since you know your function you would get a more accurate answer by performing the integration of the function. But if you did not know the function you would use this method or any other numerical integration method.
From calculus we know that an actual integral is just a reimann sum where the width of each interval is infinitely small, so this should be a valid approximation
I have a parabola y=a*(x-alpha)*(x-beta) where a, alpha, beta are known
let x1,y1 be initial points on the above parabola. I now have to cover a distance D along the arc from the initial point. The aim is to get the final position on the parabola after covering the required Arc distance.
One method is to keep checking from initial position to end point of parabola and check whether the integral is equal to the required distance. But this algorithm takes ages to complete as I need to do this many times.
Is there a faster way to do this ?
OK, a line distance on a parabola is not obtained by integration per se. I replaced your a with \gamma for convenience.
y = gamma(x-alpha)(x-beta) -> gamma*x^2 + (-gamma*alpha - gamma*beta)*x + alpha*beta*gamma
Substitute:
a = gamma
b = -gamma*alpha - gamma*beta
c = alpha*beta*gamma
So one can write:
y = a*x^2 + b*x + c
From this formula (parabola) the focal point f is determined by:
f = (1-D)/(4*a) - (-D)/(4*a)
In which
D = b^2 - 4*a*c
Now first define p as the perpendicular distance from a location on the curve to the vertical axis of symmetry x_p:
p = -b / (2*a) - x_p;
From this the arc length is calculated by:
h = p/2
q = sqrt(f^2+h^2)
s = h*q/f + f* ln (h*q/f)
In which s is the paramatric representations of your curve. The length of a curve is found by using two points s and then the distance between those points, so:
Distance = s_1 - s_2 = (h_1*q_1/f + f * ln (h_1*q_1/f)) - (h_2*q_2/f + f * ln (h_2*q_2/f))
With some simplifications:
Distance = (h_1*q_1 - h_2*q_2)/f + f* ln (( h_1 + q_1) / (h_2 + q_2))
To implement it fully in MatLab is up to you for now. Let me know if you run into any problems.
Good luck and have fun!
This is a nonlinear equation: you are given x1,y1 and seek x2, y2. x2 and y2 must satisfy the parabola equation and they must satisfy the arclength equation along a curve. http://en.wikibooks.org/wiki/Calculus/Arc_length
So that integral gives you the second equation. Now you have two equations and two unknowns - look up matlab fsolve http://www.mathworks.se/help/optim/ug/fsolve.html
assume the integral can't solve analytically (Can it?) then just solve it numerically using integrate - should still be much faster than just brute force search...
If you need to do this many times, make sure that the previous solutions are given as initial guesses for the next fsolve / integrate calls
I have no idea what are you using this for. But let me try to help you anyway
Well, this problem is not so hard to solve anallitically. I mean, it's just a simple line integral of 1 over a parabole. And good for you, it's been done before. http://en.wikipedia.org/wiki/Arc_length#Length_of_an_arc_of_a_parabola
All you need to do is to find the symmetry axis, not hard, X = (alpha + beta)/2, and the focus length (a little bit trickier, but if you google that I'm sure you'll find it). And I found it for you. https://math.stackexchange.com/questions/574688/what-is-the-focal-width-of-a-parabola shows the canonical form of the parabole, so all you need to do is to factor your equation (http://www.math-prof.com/Alg2/Alg2_Ch_40.asp)
Now the equation you need to solve is not pretty. Put everything in terms of p and let MatLab solve it for you (it shouldn't take forever).
I have a function y=0.05*x.^2 - 0.24*x+(1/(x.^2+1)).
1) I want to find the slope for x [-4,4] , so I do
syms x;
y=0.05*x.^2 - 0.24*x+(1/(x.^2+1))
der=diff(y)
matrix=subs(der,x,-4:4)
and I am finding the values of y'(x) for the different values of x.
(the result is : -0.6123 -0.4800 -0.2800 0.1600 -0.2400 -0.6400 -0.2000 0 0.1323)
Now, I want to determine all the peaks and valleys of the slope.
To find this , I take from the results that for x=3 i have y'(3)=0 => I have a critical point.
So, to find the peaks and valleys I need to see the sign left and right from point 3,right?
So, for x=-4,-2 =>valley , x=-2,-1 peak, x=-1,0 valley, x=0,2 valley , x=2,4 peak.
Is this right? Also,for plotting the slope I use ezplot(der) ?
2) I need to find the drop of the slope (difference between largest ans smallest value of y).
How can I find that, since y is symbolic?
3) If I want to find the slope in degrees, how can I do it?
4) If I have x and t data (position and time) and I want to compute the velocity, I just do?
v=x./t;
result=diff(v)
--------UPDATE---------------
For my last question i have:
time=linspace(0,1.2,13);
position=[41,52,61,69,73,75,74,66,60,55,43,27,27];
v=position./time;
vel=diff(v)
plot(time,vel)
But the problem is that vel vector results in 1x12 vector instead 1x13.Why is that?
I am not really familiar with matlab, but I am going to give you some pointers with respect to the math. You define:
y(x) = 0.025*x^2 - 0.24*x + (1/(x^2+1))
This is the blue curve in the added picture. We can take the derivative with respect to x to find:
dy(x)/dx = 0.1*x - 0.24 - (2*x/(1+x^2)^2)
which is the purple curve. I do not really know what you mean with 'peaks' and 'valleys' but if you mean maxima and minima of y(x) respectively than your answer is incorrect. Maxima or minima in y(x) can be found by finding the values of x where the derivative dy/dx is zero. You can confirm this by looking at the picture. At x=3 red curve is zero because y(x) has a minimum there. (Note that by finding a point x where the derivative is zero, does not tell you whether it is in fact a maximum or a minimum, just that it is an extremum).
2) You can find the drop in the curve as follows. First determine the values of x of the maximum and the minimum x1 and x2 (i.e. solve dy(x)/dx == 0). The drop is then abs( y(x1) - y(x2) ).
3) Officially the curve does not have one slope - it is curved so its slope varies with x. However if you mean the average slope between the max and min than it is simple geometry. You have the displacement in x and y, look into the function tan and you will be able to find the answer.
Good luck
I want to compute the gradient of the electrostatic potential of combination of 4 charges located at (1,1,0), (1,-1,0), (-1,1,0) and (-1,-1,0). How can I use the symbolic toolbox in MATLAB to achieve this?
My electromagnetics is rusty, but your question has a simple analytical solution.
The electric potential is:
and this is what it looks like on the plane z=0
Now the gradient is
and noting that
you can easily apply the above to all the terms in the equation of the gradient to get a closed form solution that can be easily plotted.
In MATLAB:
Here's an example that shows you how to perform the above partial differentiation in MATLAB. You can then build upon this to derive the full solution. I'll leave that upto you.
syms x y z x0 y0 z0
diff(1/sqrt((x-x0)^2+(y-y0)^2+(z-z0)^2),x)
ans =
-(x - x0)/((x - x0)^2 + (y - y0)^2 + (z - z0)^2)^(3/2)