Error in mat2str function
7 views (last 30 days)
Show older comments
Hi, I have the following code that implements the Newton method for nonlinear systems. This code works in matlab 2010 but not in 2013.Tells me that's a input matrix must be numeric
if true
clear;
clc;
syms x1 x2 x3 x4 x5 x6 x7 x8 x9 x y z w t;
vars=input('Tell vector (line) of the variables=');
F=input('Tell vector (line) of the equations =');
J=jacobian(F,vars);
X=input('Tell vector (line) initial approach =');
erro=input('Tell precision error =');
itmax=input('Enter the maximum number of iterations =');
e=erro+1;
it=0;
while(e>erro && it<itmax)
X0=X;
FX=subs(F,vars,X)
JX=subs(J,vars,X)
delta=JX\(-FX)'
X=X+delta'
e=norm(delta,inf)
disp(['X(transposed)=',mat2str(X),' Error=',num2str(e),' iteration=',num2str(it)]);
it=it+1;
end
end
How can I fix this?
The input is:
[x1 x2]
[x1^2+(x2-2)^2-9 (x1-1)^2+x2^2-2]
[-0.1 -1]
0.01
20
and the output should be:
X=[-4.05844155882936e-06 -1.00000202922078] Error=0.0031209 iteration=2
Can anyone help me? I appreciate any help...
Thanks
0 Comments
Answers (1)
Prateekshya
on 9 Jul 2024
Hi Leandro,
As per my understanding the error is caused since you are passing symbolic variables to the "mat2str" and the "num2str" functions. These functions are updated and they take only numeric values as input. The following change in your code gives the correct output.
disp(['X(transposed)=',mat2str(double(X)),' Error=',num2str(double(e)),' iteration=',num2str(it)]);
The "double" function ensures that the symbolic variables are converted to corresponding double values leading to error free execution of the code.
I hope this helps!
Thank you.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!