fzero or solve a file full of relations
2 views (last 30 days)
Show older comments
Hi all-
I was playing around with fzero here: http://www.mathworks.com/help/matlab/ref/fzero.html#description
Trying to get it to vary x below until y below is 0.
z=[-300.00 1082.00 7494.20];
x=1.750871192;
zprime=[z(1)*x/10000 z(2)*x/10000 z(3)*x/10000];
a=[1 zprime(3) zprime(2) zprime(1)]';
B=[1.637510856e-06 -2.252209129e-05 0.0001949906158 9.284333582e-06
-2.252209129e-05 0.217829072 0.03330113194 0.0449503853
0.0001949906158 0.03330113194 0.5146272212 0.03759452473
9.284333582e-06 0.0449503853 0.03759452473 0.7076169167];
Ba = B*a;
atBa = a'*Ba;
y=atBa-1;
I think it can be done with fzero which requires no license for solve. But I do have the license for solve if that's the way to go. I hate to fight over limited licenses :)
Hope this one isn't too tough. Need to call it many times, quickly.
Thanks a ton.
Dave
0 Comments
Accepted Answer
Torsten
on 9 Jun 2016
Try
function main
x0=1;
sol=fzero(@f,x0);
function y=f(x)
z=[-300.00 1082.00 7494.20];
B=[1.637510856e-06 -2.252209129e-05 0.0001949906158 9.284333582e-06
-2.252209129e-05 0.217829072 0.03330113194 0.0449503853
0.0001949906158 0.03330113194 0.5146272212 0.03759452473
9.284333582e-06 0.0449503853 0.03759452473 0.7076169167];
for k=1:length(x)
xk=x(k);
a=[1 z(3)*xk/10000 z(2)*xk/10000 z(1)*xk/10000]';
y(k)=a'*B*a-1;
end
Best wishes
Torsten.
2 Comments
Torsten
on 9 Jun 2016
Edited: Torsten
on 9 Jun 2016
In function main, you give a starting guess for x (x0=1).
In function f, fzero wants your function to be evaluated at several values of x ( x is a vector here). The for-loop evaluates your function element-wise (in elements xk of the vector x), saves the results in the array y and returns y to fzero.
Best wishes
Torsten.
More Answers (1)
Matt J
on 8 Jun 2016
Edited: Matt J
on 8 Jun 2016
You could use either one, but it will be overkill regardless of which one you choose. The problem that you have shown is a 1D quadratic equation in x. You could solve it by hand or with roots().
2 Comments
Matt J
on 9 Jun 2016
The coefficients would be obtained by expanding out the terms of atBa. But if the true function is not a polynomial, roots will not solve it.
See Also
Categories
Find more on Optimization in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!