solving a multi variables function equal to zero uzing fzero

34 ビュー (過去 30 日間)
sarra aloui
sarra aloui 2019 年 5 月 26 日
編集済み: John D'Errico 2019 年 5 月 30 日
i was trying to solve this code
format loose
format compact
format long
m=[ 4000 50 ] ;
s= [400 5 ] ;
r=[1 0.5 , 0.5 1 ];
q=[1 1 ] ;
ls=[];
n=length(m) ;
for i = 1 : n
eval(sprintf('syms x%i,',i));
eval(sprintf('x(%i) = x%i;', i, i));
end
Y= @(x1, x2) 29-(6*x(1))-(18*x(2));
for i=1:n
if i<n
xi=m(i);
disp(x);
disp(i);
else
last=m(n)
xi = fzero(Y,last)
%=>>>>>>>>>>>>>> 5
end
end
i was trying to obtain the value of the last varible in the function Y=0 but i am getting this error :
Error using fzero (line 328)
Function value at starting guess must be finite and real.

採用された回答

John D'Errico
John D'Errico 2019 年 5 月 26 日
編集済み: Walter Roberson 2019 年 5 月 27 日
As much as you want it to do so, fzero is not designed to solve multi-variable problems. Software cannot be made to do what it was not written to do.
You can instead use a tool like fsolve. Or solve. you could even make fminsearch do it for you. But fzero? Not really an option. (Yes, with a fairly great deal of effort, you might be able to cobble something up, using nested calls. Pray tell, why would you even want to do so?)
Use the correct tool for a problem. Would you use a hammer to cut a piece of wood in half? A saw makes more sense.
  3 件のコメント
John D'Errico
John D'Errico 2019 年 5 月 30 日
編集済み: John D'Errico 2019 年 5 月 30 日
So then it is not a multi-variable problem? I'm trying to read your code, but it is highly confusing. You create symbolic variables for no reason at all.
Is all that you want to do is solve the function Y, with respect to x(2), while fixing x(1) to be m(1)? Forgetting that the problem is trivial, and that you could solve it using solve anyway, but I'll assume your real problem is more complicated.
Y = @(x1,x2) 29 - 6*x1 - 18*x2;
So y is a function of TWO variables, x1, and x2. Now, we wish to fix x1 to have some value, while solveing for the value of x2. Note there was no need to do anything silly, like create symbolic variables using eval. No symbolic variables will be created here.
Assume the desired value for x1 is m(1).
m=[ 4000 50 ] ;
x2 = fzero(@(x2) Y(m(1),x2),-1000)
x2 =
-1331.72222222222
In fact, we know the solution would be:
(29 - 6*m(1))/18
ans =
-1331.72222222222
As you can see, fzero found it too.

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by