필터 지우기
필터 지우기

fzero or solve a file full of relations

조회 수: 1 (최근 30일)
David Pesetsky
David Pesetsky 2016년 6월 8일
편집: Torsten 2016년 6월 9일
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

채택된 답변

Torsten
Torsten 2016년 6월 9일
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
David Pesetsky
David Pesetsky 2016년 6월 9일
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 2016년 6월 9일
편집: Torsten 2016년 6월 9일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Matt J
Matt J 2016년 6월 8일
편집: Matt J 2016년 6월 8일
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
David Pesetsky
David Pesetsky 2016년 6월 9일
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 2016년 6월 9일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Optimization에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by