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).
Related
I'm trying to solve this equation for x (or y). Is there some way to do it in Matlab? The "simplify" function (even with "steps",100,"All", true) doesn't do it.
-k+Sqrt[x^2+y^2]+Sqrt[(x-ax)^2+y^2]+Sqrt[(x-bx)^2+(y-by)^2]
A symbolic solution does not exist in the general case, because the problem amounts to solving an 8th degree algebraic equation.
Defining the points M(x,y), O(0,0), A(ax,0) and B(bx,by) in the plane, the equation can be written as MO + MA + MB = k. The solution set M is the locus of points with constant sum of distances to 3 fixed points. This is called a 3-ellipse which is a curve of algebraic degree 2^3 = 8 in the general case.
If F is the Fermat point of triangle OAB and f = FO + FA + FB, then no solutions exist for k < f. More material and links related to 3-ellipses can be found on this multiellipse page.
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'm not familiar with expert math. so I don't know where to start from.
I have get a some article like this. I am just following this article description. But this is not easy to me.
But I'm not sure how to make just one polynomial equation(or something like that) from above 4 polynomial equations. Is this can be possible way?
If yes, Would you please help me how to get a polynomial(or something like equation)? If not, would you let me know the reason of why?
UPDATE
I'd like to try as following
clear all ;
clc
ab = (H' * H)\H' * y;
y2 = H*ab;
Finally I can get some numbers like this.
So, is this meaning?
As you can see the red curve line, something wrong.
What did I miss anythings?
All the article says is "you can combine multiple data sets into one to get a single polynomial".
You can also go in the other direction: subdivide your data set into pieces and get as many separate ones as you wish. (This is called n-fold validation.)
You start with a collection of n points (x, y). (Keep it simple by having only one independent variable x and one dependent variable y.)
Your first step should be to plot the data, look at it, and think about what kind of relationship between the two would explain it well.
Your next step is to assume some form for the relationship between the two. People like polynomials because they're easy to understand and work with, but other, more complex relationships are possible.
One polynomial might be:
y = c0 + c1*x + c2*x^2 + c3*x^3
This is your general relationship between the dependent variable y and the independent variable x.
You have n points (x, y). Your function can't go through every point. In the example I gave there are only four coefficients. How do you calculate the coefficients for n >> 4?
That's where the matricies come in. You have n equations:
y(1) = c0 + c1*x(1) + c2*x(1)^2 + c3*x(1)^3
....
y(n) = c0 + c1*x(n) + c2*x(n)^2 + c3*x(n)^3
You can write these as a matrix:
y = H * c
where the prime denotes "transpose".
Premultiply both sides by transpose(X):
transpose(X)* y = transpose(H)* H * c
Do a standard matrix inversion or LU decomposition to solve for the unknown vector of coefficients c. These particular coefficients minimize the sum of squares of differences between the function evaluated at each point x and your actual value y.
Update:
I don't know where this fixation with those polynomials comes from.
Your y vector? Wrong. Your H matrix? Wrong again.
If you must insist on using those polynomials, here's what I'd recommend: You have a range of x values in your plot. Let's say you have 100 x values, equally spaced between 0 and your max value. Those are the values to plug into your H matrix.
Use the polynomials to synthesize sets of y values, one for each polynomial.
Combine all of them into a single large problem and solve for a new set of coefficients. If you want a 3rd order polynomial, you'll only have four coefficients and one equation. It'll represent the least squares best approximation of all the synthesized data you created with your four polynomials.
I have a symbolic function, whose zeros I am particular interested in knowing. I have searched through google, trying to find something related to my query, but was unsuccessful.
Could someone please help me?
EDIT:
T(x,t) = 72/((2*n+1)^2*pi^3)*(1 - (2*n+1)^2*pi^2*t/45 + (2*n+1)^4*pi^4*t^2/(2*45^2) - (2*n+1)^6*pi^6*t^3/(6*45^3))*(2*n+1)*pi*x/3;
for i=1:1:1000
T_new = 72/((2*i+1)^2*pi^3)*(1 - (2*i+1)^2*pi^2*t/45 + (2*i+1)^4*pi^4*t^2/(2*45^2) - (2*i+1)^6*pi^6*t^3/(6*45^3))*(2*i+1)*pi*x/3;
T = T + T_new;
end
T = T - 72/((2*n+1)^2*pi^3)*(1 - (2*n+1)^2*pi^2*t/45 + (2*n+1)^4*pi^4*t^2/(2*45^2) - (2*n+1)^6*pi^6*t^3/(6*45^3))*(2*n+1)*pi*x/3;
T = T(1.5,t);
T_EQ = 0.00001
S = solve(T - T_EQ == 0,t);
The problem that I get is that S is an a vector which contains imaginary numbers. I expected a real number, because I am trying to calculate a time.
Here is a little background as to what I am trying to do:
http://hans.math.upenn.edu/~deturck/m241/solving_the_heat_eqn.pdf
In the given link is the heat equation solved for a particular one-dimensional case. The temperature distribution, that satisfies the prescribed boundary and initial conditions, is given on page 50, I believe.
What I would like to do is find the time at which the one-dimensional object equilibrates with the environment, which is held at a constant temperature of T=0. As far as I know, the easiest way to do this would be to use the Taylor expansion of the exponential function, using only the first few terms, because I expect the equilibrium time to be relatively short; and then use the small angle approximation for the sine function, because the rod has a relatively small length. Doing just this, I made a for loop to generate terms just as the summation function would--as you can see, I used 1000 terms.
Does what I am doing seem wrong to anyone? If there is a better method, could someone please recommend it?
You shouldn't be surprised to see imaginary roots provided that at least one root is real and positive, corresponding to your time. The question is if the time makes any sense due to the approximations that you're making. Have you plotted the the actual function to get a rough approximation for where the zero is?
I can't really comment on the particular problem you're trying to solve. You need to make sure that you're using enough Taylor expansion terms an that they are accurate for the domain. Have you tried this leaving in the exp and/or sin? Is there any reason that you can't just use zero? And have you checked that your summation has converged after 1,000 terms? Or does it converge much sooner or not at all?
The main question is why are you using symbolic math at all to solve this? This seems like a numeric problem unless you're experiencing overflow/underflow issues in your summation. You can find the zero using fzero in this case:
N = 32; % Number of terms in summation
x = 1.5;
T_EQ = 1e-5;
n = (2*(0:N)+1)*pi;
T = #(t)sum((72./n.^3).*exp(-n.^2*t/45).*sin(n*x/3))-T_EQ;
S = fzero(T,[0 1e3]) % Bounds around a root guarantees solution if function monotonic
which returns
S =
56.333877640358708
If you're going to use solve, I'd do something like the following to avoid for loops:
syms t
N = 32;
x = 1.5;
T_EQ = 1e-5;
n = (2*sym(0:N)+1)*sym(pi);
T(t) = sum((72./n.^3).*exp(-n.^2*t/45).*sin(n*x/3));
S = double(solve(T-T_EQ==0,t))
or, using symsum:
syms n t
N = 32;
x = 1.5;
T_EQ = 1e-5;
T(t) = symsum((72/(pi*(2*n+1))^3)*exp(-(pi*(2*n+1))^2*t/45)*sin(pi*(2*n+1)*x/3),n,0,N);
S = double(solve(T-T_EQ==0,t))
Lastly, your symbolic solutions are not even exact as some your pi variables are being converted to rational approximations. pi is floating point. Things like pi*t are generally safe if t is symbolic, because pi will be recognized as such. However, pi^2 is calculated in floating-point before being converted to symbolic due to order of operations. In general your should use sym('pi') or sym(pi) in symbolic expressions.
Assuming you have a polynomial or trigonometric function of x or y, and what you mean by "zeros" is the values where the function crosses the axis, i.e., either x or y is zero, you can call the value of the function when a variable is 0. An example:
syms x y
f=-cos(x)*exp(-(x^2)/40);
ezsurf(f,[-10,10])
F=matlabFunction(f,'vars',{[x]});
F([0])
The ezsurf just visualizes the plot. If you want a function of both x and y, you do something like the following:
syms x y
f=-cos(x)*cos(y)*exp(-(x^2+y^2)/40);
ezsurf(f,[-10,10])
F=matlabFunction(f,'vars',{[x,y]});
for y=0
solve(f)
end
This will give you the value of the function for which integer multiples of x correspond to zero points for y (values of the function that are on the y=0 plane).
I would like to use a MATLAB function to find the minimum length between a point and a curve? The curve is described by a complicated function that is not quite smooth. So I hope to use an existing tool of matlab to compute this. Do you happen to know one?
When someone says "its complicated" the answer is always complicated too, since I never know exactly what you have. So I'll describe some basic ideas.
If the curve is a known nonlinear function, then use the symbolic toolbox to start with. For example, consider the function y=x^3-3*x+5, and the point (x0,y0) =(4,3) in the x,y plane.
Write down the square of the distance. Euclidean distance is easy to write.
(x - x0)^2 + (y - y0)^2 = (x - 4)^2 + (x^3 - 3*x + 5 - 3)^2
So, in MATLAB, I'll do this partly with the symbolic toolbox. The minimal distance must lie at a root of the first derivative.
sym x
distpoly = (x - 4)^2 + (x^3 - 3*x + 5 - 3)^2;
r = roots(diff(distpoly))
r =
-1.9126
-1.2035
1.4629
0.82664 + 0.55369i
0.82664 - 0.55369i
I'm not interested in the complex roots.
r(imag(r) ~= 0) = []
r =
-1.9126
-1.2035
1.4629
Which one is a minimzer of the distance squared?
subs(P,r(1))
ans =
35.5086
subs(P,r(2))
ans =
42.0327
subs(P,r(3))
ans =
6.9875
That is the square of the distance, here minimized by the last root in the list. Given that minimal location for x, of course we can find y by substitution into the expression for y(x)=x^3-3*x+5.
subs('x^3-3*x+5',r(3))
ans =
3.7419
So it is fairly easy if the curve can be written in a simple functional form as above. For a curve that is known only from a set of points in the plane, you can use my distance2curve utility. It can find the point on a space curve spline interpolant in n-dimensions that is closest to a given point.
For other curves, say an ellipse, the solution is perhaps most easily solved by converting to polar coordinates, where the ellipse is easily written in parametric form as a function of polar angle. Once that is done, write the distance as I did before, and then solve for a root of the derivative.
A difficult case to solve is where the function is described as not quite smooth. Is this noise or is it a non-differentiable curve? For example, a cubic spline is "not quite smooth" at some level. A piecewise linear function is even less smooth at the breaks. If you actually just have a set of data points that have a bit of noise in them, you must decide whether to smooth out the noise or not. Do you wish to essentially find the closest point on a smoothed approximation, or are you looking for the closest point on an interpolated curve?
For a list of data points, if your goal is to not do any smoothing, then a good choice is again my distance2curve utility, using linear interpolation. If you wanted to do the computation yourself, if you have enough data points then you could find a good approximation by simply choosing the closest data point itself, but that may be a poor approximation if your data is not very closely spaced.
If your problem does not lie in one of these classes, you can still often solve it using a variety of methods, but I'd need to know more specifics about the problem to be of more help.
There's two ways you could go about this.
The easy way that will work if your curve is reasonably smooth and you don't need too high precision is to evaluate your curve at a dense number of points and simply find the minimum distance:
t = (0:0.1:100)';
minDistance = sqrt( min( sum( bxsfun(#minus, [x(t),y(t)], yourPoint).^2,2)));
The harder way is to minimize a function of t (or x) that describes the distance
distance = #(t)sum( (yourPoint - [x(t),y(t)]).^2 );
%# you can use the minimum distance from above as a decent starting guess
tAtMin = fminsearch(distance,minDistance);
minDistanceFitte = distance(tAtMin);