Effacer les filtres
Effacer les filtres

Trouble using jacobian inside a function

1 vue (au cours des 30 derniers jours)
David Hagan
David Hagan le 11 Fév 2012
Commenté : Jan le 10 Nov 2017
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

Réponses (2)

Walter Roberson
Walter Roberson le 11 Fév 2012
The function jacobian is part of the symbolic toolbox and applies only to symbolic expressions.
Perhaps you want the gradient() function?

David Hagan
David Hagan le 11 Fév 2012
Thanks for the help. I ended up just defining the symbols inside each method instead and it works fine!
  2 commentaires
omkar jagan
omkar jagan le 10 Nov 2017
how did u do that. Can u explain with some small syntax.
Jan
Jan le 10 Nov 2017
@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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Mathematics dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by