Change equation in fsolve inside a loop

1 回表示 (過去 30 日間)
esbolico
esbolico 2012 年 11 月 5 日
Hi All,
I am having some trouble with the following problem:
I create some symbolic equations from a complex mathematical model (the following equations are just to show my problem) as follows:
syms x1 x2 x3
EqsToSolve= [x1+x2*x2+7*x3 ;log(x1)+exp(x2)+x3 ;3*x1+x2*x3+2]
what I do now is copying this to a M-File that looks like
function y=myfun(x)
x1=x(1)
x2=x(2)
x3=x(3)
y=[x1+x2*x2+7*x3 ;log(x1)+exp(x2)+x3 ;3*x1+x2*x3+2]
end
and I solve it using fsolve as usual.
With the solution of this system I update my model and create another set of equations such as:
EqsToSolve2= [54*x1^2+x2+x3 ;(x2)+x3+32 ;3*x1+x2*x3+2+log(x3)]
and I copy that to myfun.m and so on till convergence. The problem is that convergence can arrive after 1000 iterations and this procedure is slow.
IS there any way to solve this problem, that is, to achieve something like:
while not_converged
create_updated_model
solve new_equation
end
or how to change the equation in the myfun.m inside a while o for loop.
Thank you very much!! Any help will be appreciated!!

採用された回答

Conrad
Conrad 2012 年 11 月 5 日
Try using anonymous functions:
f1 = @(x) [x(1)+x(2)*x(2)+7*x(3) ;log(x(1))+exp(x(2))+x(3);3*x(1)+x(2)*x(3)+2];
f2 = @(x) [54*x(1)^2+x(2)+x(3) ;(x(2))+x(3)+32 ;3*x(1)+x(2)*x(3)+2+log(x(3))];

その他の回答 (2 件)

esbolico
esbolico 2012 年 11 月 5 日
Hi Conrad,
thank you for your answer. I'll check it and keep you informed!!!

esbolico
esbolico 2012 年 11 月 5 日
I still do not know how to make it work. The thing is I get my model as a function of x1,x2,x3 or x,y,z. How can I change it to get x(1),x(2),x(3)??
I'll try to explain it better:
After running my code I get a symbolic variable such as
EqsToSolve= [x1+x2*x2+7*x3 ;log(x1)+exp(x2)+x3 ;3*x1+x2*x3+2]
as far as I understand you are telling me to change manually x1-->x(1), x2-->x(2) and so on
  2 件のコメント
Conrad
Conrad 2012 年 11 月 7 日
f1 = @(x1,x2,x3) [x1+x2*x2+7*x3 ;log(x1)+exp(x2)+x3;3*x1+x2*x3+2];

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeNumeric Solvers についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by