Trouble using jacobian inside a function
2 ビュー (過去 30 日間)
古いコメントを表示
I wrote a function that uses the Newton-Raphson method to solve a system of n equations and n uknowns (n<= 4 at the moment) but for some reason, I started getting an error concerning using the jacobian matlab function inside my function. The error reads:
??? Undefined function or method 'jacobian' for input arguments of type 'double'.
Error in ==> NewtRap at 21
J = jacobian(f0,v)
My function code is:
function [ answer ] = NewtRap( )
%UNTITLED2 Summary of this function goes here
% Detailed explanation goes here
Input %%%
global syms x y z w
fprintf('\n')
disp(' 1 ) One Equation ')
disp(' 2 ) Two Equations ')
disp(' 3 ) Three Equations ')
disp(' 4 ) Four Equations ')
method = input('\n Choose the number of Equations that you have: ');
if method == 1
f1 = input('Enter your equation. Use x as the variable: ');
x0 = input('Enter x0: ');
tol = input('Enter the maximum tolerance: ');
v = [ x ]; %This is used to define the jacobian
f0 = [f1]; %This is used to define the jacobian
J = jacobian(f0,v);
J_inv = inv(J);
delta_x = -J_inv*f0;
%While loop
dx = [];
xx = [];
xx(:,1) = x0;
tol1 = 100;
i = 1;
while tol1 > tol
i = i + 1;
dx(:,i-1) = subs(delta_x,v,xx(:,i-1));
xx(:,i) = xx(:,i-1) + dx(:,i-1);
tol1 = abs(xx(1,i) - xx(1,i-1));
end
answer = xx(:,end);
elseif method == 2
f1 = input('Enter your equation. Use x and y as the variables: ');
f2 = input('Enter your equation. Use x and y as the variables: ');
x0 = input('Enter initial values as [x0 ;y0]: ');
tol = input('Enter the maximum tolerance: ');
v = [x y]; %This is used to define the jacobian
f0 = [f1;f2]; %This is used to define the jacobian
J = jacobian(f0,v);
J_inv = inv(J);
delta_x = -J_inv*f0;
%While loop
dx = [];
xx = [];
xx(:,1) = x0;
tol1 = 100;
tol2 = 100;
i = 1;
while tol1 > tol || tol2 > tol
i = i + 1;
dx(:,i-1) = subs(delta_x,v,xx(:,i-1));
xx(:,i) = xx(:,i-1) + dx(:,i-1);
tol1 = abs(xx(1,i) - xx(1,i-1));
tol2 = abs(xx(2,i) - xx(2,i-1));
end
answer = xx(:,end);
elseif method == 3
f1 = input('Enter your equation. Use x, y, and z as the variables: ');
f2 = input('Enter your equation. Use x, y, and z as the variables: ');
f3 = input('Enter your equation. Use x, y, and z as the variables: ');
x0 = input('Enter initial values as [x0;y0;z0]: ');
tol = input('Enter the maximum tolerance: ');
v = [x y z]; %This is used to define the jacobian
f0 = [f1;f2;f3]; %This is used to define the jacobian
J = jacobian(f0,v);
J_inv = inv(J);
delta_x = -J_inv*f0;
%While loop
dx = [];
xx = [];
xx(:,1) = x0;
tol1 = 100;
tol2 = 100;
tol3 = 100;
i = 1;
while tol1 > tol || tol2 > tol || tol3 > tol
i = i + 1;
dx(:,i-1) = subs(delta_x,v,xx(:,i-1));
xx(:,i) = xx(:,i-1) + dx(:,i-1);
tol1 = abs(xx(1,i) - xx(1,i-1));
tol2 = abs(xx(2,i) - xx(2,i-1));
tol3 = abs(xx(3,i) - xx(3,i-1));
end
answer = xx(:,end);
elseif method == 4
f1 = input('Enter your equation. Use x, y, z, and w as the variables: ');
f2 = input('Enter your equation. Use x, y, z, and w as the variables: ');
f3 = input('Enter your equation. Use x, y, z, and w as the variables: ');
f4 = input('Enter your equation. Use x, y, z, and w as the variables: ');
x0 = input('Enter initial values as [x0;y0;z0;w0]: ');
tol = input('Enter the maximum tolerance: ');
v = [x y z w]; %This is used to define the jacobian
f0 = [f1;f2;f3;f4]; %This is used to define the jacobian
J = jacobian(f0,v);
J_inv = inv(J);
delta_x = -J_inv*f0;
%While loop
dx = [];
xx = [];
xx(:,1) = x0;
tol1 = 100;
tol2 = 100;
tol3 = 100;
tol4 = 100;
i = 1;
while tol1 > tol || tol2 > tol || tol3 > tol || tol4 > tol
i = i + 1;
dx(:,i-1) = subs(delta_x,v,xx(:,i-1));
xx(:,i) = xx(:,i-1) + dx(:,i-1);
tol1 = abs(xx(1,i) - xx(1,i-1));
tol2 = abs(xx(2,i) - xx(2,i-1));
tol3 = abs(xx(3,i) - xx(3,i-1));
tol4 = abs(xx(4,i) - xx(4,i-1));
end
answer = xx(:,end);
elseif method ~= 1 || method ~= 2 || method ~= 3 || method ~= 4
fprintf('This option is not currently available\n');
end
end
0 件のコメント
回答 (2 件)
David Hagan
2012 年 2 月 11 日
2 件のコメント
Jan
2017 年 11 月 10 日
@omkar jagan: David is not active in the forum for 5 years now. Most likely he will not see your question or even remember, what he did some years ago. I suggest to open a new thread and ask a specific question about your own problem.
参考
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!