Clear Filters
Clear Filters

fzero or solve a file full of relations

2 views (last 30 days)
Hi all-
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

Accepted Answer

Torsten
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
David Pesetsky
David Pesetsky on 9 Jun 2016
That works. Matches excel's goal seek pretty close.
I don't get the use of the For loop, or how X is getting assigned.
Torsten
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.

Sign in to comment.

More Answers (1)

Matt J
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
David Pesetsky
David Pesetsky on 9 Jun 2016
Sorry, I shortened the real problem here to include the linear only terms. It can go up to quadratic, and is transcendental. I guess maybe roots will still solve it? But how to get the coefficients from my matrices to input into roots?
I wouldn't mind some brute force if needed.
Matt J
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.

Sign in to comment.

Categories

Find more on Optimization 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!