How I can correct this error? "Undefined operator '/' for input arguments of type 'function_handle'."

1 回表示 (過去 30 日間)
I'm trying to solve system of non-linear equation using Newton raphson method.
I'm coded right, but it keep me saying "Undefined operator '/' for input arguments of type 'function_handle'."
Any one can help me with it? The error is on line 19.
Heres the full code:
This code used to generate Jacobian vector.
function J=Derivative(F,x,xi,i)
e=0.001;
x1=x;
x2=x;
x1(i)=xi;
x2(i)=xi+e;
J=(F(x2)-F(x1))/e;
end
This is the full code I,m try to solve. In this code I have 8 system of equation, which are 5 of them are non-linear and 3 of them are linear.
%newton raphson method
clc
clear all
R=0.314;
T=1000;
z=@(x) x(1)+x(2)+x(3)+x(4)+x(5);
F1=@(x) 19720+(R*T*log(x(1)/z))-x(6)-4*x(7);
F2=@(x) -1920420+(R*T*log(x(2)/z))-2*x(7)-x(8);
F3=@(x) -200240+(R*T*log(x(3)/z))-x(6)-x(8);
F4=@(x) -395790+(R*T*log(x(4)/z))-x(6)-2*x(8);
F5=@(x) (R*T*log(x(2)/z))-2*x(7);
F6=@(x) x(1)+x(3)+x(4)-2;
F7=@(x) 4*x(1)+2*x(2)+2*x(5)-14;
F8=@(x) x(2)+x(3)+2*x(4)-3;
x=[0.25; 0.198; 0.222; 0.78; 0.7892; 2000; 3000; 5000];
for i=1:10
F = [F1(x);F2(x);F3(x);F4(x);F5(x);F6(x);F7(x);F8(x)]; %The error is on this line
for k=nume1(x)
for j=numel(x)
Jacobian(k,j)=Derivative(eval(['F' num2str(k)]),x,x(j),j);
end
end
x=x-inv(Jacobian)*F;
end
  2 件のコメント
madhan ravi
madhan ravi 2020 年 9 月 21 日
there's already an inbuilt function named as jacobian() , you have spelling error in j = nume1() should be numel() , after defining z , replace the other z's with z(x).
tolossa kebede
tolossa kebede 2020 年 9 月 22 日
Thank you for your help madhan ravi, it's working.

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

採用された回答

Steven Lord
Steven Lord 2020 年 9 月 21 日
z=@(x) x(1)+x(2)+x(3)+x(4)+x(5);
F1=@(x) 19720+(R*T*log(x(1)/z))-x(6)-4*x(7);
You can't divide x(1) by the function handle z.
You could divide x(1) by the value obtained by evaluating the function handle z at x.
s = @sin;
c = @cos;
t1 = @(x) s(x)./c(x); % will work
t2 = @(x) s./c; % will fail when called
t1(2)-tan(2)
t2(2) % error

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeNewton-Raphson Method についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by