solving a function equal to zero
조회 수: 52 (최근 30일)
이전 댓글 표시
i am trying to run this code to obtain the last value to the function y=0
format loose
format compact
format long
m=[ 4000 50 ] ;
s= [400 5 ] ;
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
temp =(-diff(Y,x(i)));
ls=[ls, temp];
end
for i=1:n
if i<n
xi=m(i);
disp(x);
disp(i);
else
last=4000 ;
xi = fzero(Y,last) ;
end
end
i am trying to define the last value of the function y=0 using initial guess but i am getting this error
Error using fzero (line 328)
Function value at starting guess must be finite and real.
댓글 수: 0
채택된 답변
dpb
2019년 5월 26일
편집: dpb
2019년 5월 27일
fsolve does the work for you...if you define the functional correctly--
fnY= @(x) (29-(6*x(1))-(18*x(2)));
opt= optimoptions('fsolve','algorithm','levenberg-marquardt');
>> fsolve(Y,[0 0],opt)
Equation solved.
fsolve completed because the vector of function values is near zero
as measured by the default value of the function tolerance, and
the problem appears regular as measured by the gradient.
<stopping criteria details>
ans =
0.4833 1.4500
>>
Of course, there are an infinite number of possible solutions; pick a value for one or the other of the two X and solve for the other.
댓글 수: 2
dpb
2019년 5월 27일
That's simply
>> x1=4000;
>> x2=(29-(6*x1))/18
x2 =
-1.3317e+03
>>
But, you can still use fsolve if must...there's just one variable to solve for, however...
>> Y= @(x) (29-(6*x1)-(18*x));
>> x2=fsolve(Y,[ 0],opt)
Equation solved.
fsolve completed because the vector of function values is near zero
as measured by the default value of the function tolerance, and
the problem appears regular as measured by the gradient.
<stopping criteria details>
x2 =
-1.3317e+03
>>
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!