Find zero of a fucntion

1 view (last 30 days)
Cecilia
Cecilia on 30 Jan 2015
Commented: Cecilia on 1 Feb 2015
Hey. I am trying to find theta that makes the function val(theta) = 0. x is a constantvector of size 2; x1 is a constant vector of size 2; x2 is a constant vector of size 2; u1 is a constant; u2 is a constant; spidi is a constant 2x2 matrix.
xt = [(1-theta)*x1(1) + theta*x2(1), (1-theta)*x1(2) + theta*x2(2)];
a = [x(1) - xt(1), x(2) - xt(2)];
b = [x1(1) - x2(1), x1(2) - x2(2)];
dQdtheta = 2*a(1)*b(1)*spidi(1) + (a(1)*b(2)+a(2)*b(1))*spidi(2) + (a(2)*b(1)+a(1)*b(2))*spidi(3) + 2*a(2)*b(2)*spidi(4);
rot = sqrt(a*spidi*a');
val = u2 - u1 + dQdtheta/(2*rot);
theta = fzero(val,theta);
Now, I know this is wrong. But how can I find the zero of val(theta) when val is a function of variables that is a function of theta, e.g., a = a(theta)? All help is appreciated. Thank you. - Cecilia

Accepted Answer

John D'Errico
John D'Errico on 30 Jan 2015
Edited: John D'Errico on 30 Jan 2015
I cannot actually show the idea in action, because you have not given us sample values of x, x1, x2, u1, u2, spidi. (Although, my spidi sense should tell me what they were. Sorry, I could not resist.)
The idea is a simple one though. You write a function that encapsulates those equations, where you will pass in all of the known constants.
function val = valfun(theta,x,x1,x2,u1,u2,spidi)
xt = [(1-theta)*x1(1) + theta*x2(1), (1-theta)*x1(2) + theta*x2(2)];
a = [x(1) - xt(1), x(2) - xt(2)];
b = [x1(1) - x2(1), x1(2) - x2(2)];
dQdtheta = 2*a(1)*b(1)*spidi(1)+ ...
(a(1)*b(2)+a(2)*b(1))*spidi(2)+a(2)*b(1)+a(1)*b(2))*spidi(3) + ...
2*a(2)*b(2)*spidi(4);
rot = sqrt(a*spidi*a');
val = u2 - u1 + dQdtheta/(2*rot);
Save this as an m-file on your search path. Now, you call fzero, passing in all of those variables.
thetafun = @(x) valfun(theta,x,x1,x2,u1,u2,spidi);
theta = fzero(thetafun,theta0)
As you can see, I've set it up as an anonymous function, so the known parameters {x,x1,x2,u1,u2,spidi} are all passed in. theta0 is the initial value for theta, whatever it is. Better yet, supply a bracket that includes the root.
I strongly recommend you plot it too. That is easily done as...
ezplot(thetafun)
Actually, do the plot before you ever even bother to try solving for a root. This would give you an intelligent choice for the starting point, even tell you if more than one root exists, etc. And it would allow you to chose a pair of points that bracket the root, making fzero more efficient and more robust.
Had I sample values of those constants, I would have done that for you myself. But I cannot do so, as my superhero powers are lacking today. :)
Another possibility is to try solving the problem symbolically. Sadly, as I suspected, there seems to be no simple analytical solution, at least the symbolic TB fails to find one.
  1 Comment
Cecilia
Cecilia on 1 Feb 2015
It worked! You ARE my superhero. I did not get the results that I wanted though. I guess I have done something wrong in the implementation of the method, and I dont think you can help me with that. Unless you really are A superhero! Thank you anyway.

Sign in to comment.

More Answers (1)

Matt J
Matt J on 30 Jan 2015
theta_root = fzero(@(theta) a(theta, u1, u2, x1,x2,spidi), theta_guess )
function val=a(theta, u1, u2, x1,x2,spidi)
xt = [(1-theta)*x1(1) + theta*x2(1), (1-theta)*x1(2) + theta*x2(2)];
a = [x(1) - xt(1), x(2) - xt(2)];
b = [x1(1) - x2(1), x1(2) - x2(2)];
dQdtheta = 2*a(1)*b(1)*spidi(1) + (a(1)*b(2)+a(2)*b(1))*spidi(2) + (a(2)*b(1)+a(1)*b(2))*spidi(3) + 2*a(2)*b(2)*spidi(4);
rot = sqrt(a*spidi*a');
val = u2 - u1 + dQdtheta/(2*rot);
end

Categories

Find more on Debugging and Analysis in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!