필터 지우기
필터 지우기

Error in mat2str function

조회 수: 10 (최근 30일)
Leandro Rodrigues
Leandro Rodrigues 2015년 3월 26일
답변: Prateekshya 2024년 7월 9일 5:39
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

답변 (1개)

Prateekshya
Prateekshya 2024년 7월 9일 5:39
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.

카테고리

Help CenterFile Exchange에서 Numbers and Precision에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by