Error using sym>convertChar (line 1580) Character vectors and strings in the first argument can only specify a variable or number. To evaluate character vectors and strings re
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I had been needed to used Marquardt's Method for Multiple Variable Functions, but the code of I search on online has a bit problems. which the code given as below:
%Marquardt's Method for Multiple Variable Functions
clc;
close all;
%Insert starting points
x01=0;
x02=0;
%Function F in Symbolic Format
a='(x1^2+x2-11)^2+(x1+x2^2-7)^2';
%Insert the desired magnitude of final direction vector (0.0001 is fine)
eserror=0.0001;
Max_Iterations=20000;
convergance_criterion=0.0001;
k=0
gamma=100
%Leave the rest unchanged
%=================================
syms x1 x2
syms alpha real %difining alpha as real prevents matlab to solve function with conj(alpha)
% Hessian Of F
A=[diff(diff(a,x1),x1),diff(diff(a,x1),x2);...
diff(diff(a,x2),x1),diff(diff(a,x2),x2)]
%First Order Derivative of F
B=[diff(a,x1);diff(a,x2)];
% pretty(simplify(A))
subs(subs(A,x1,x01),x2,x02)
mag_of_B=sqrt(subs(subs(diff(a,x1),x1,x01),x2,x02)^2+subs(subs(diff(a,x2),x1,x01),x2,x02)^2)
records=[];
while (mag_of_B>convergance_criterion)&(k<Max_Iterations)
k=k+1;
temp=([x01;x02]-inv(subs(subs(A,x1,x01),x2,x02)+gamma*eye(size(subs(subs(A,x1,x01),x2,x02))))*(subs(subs(B,x1,x01),x2,x02)))'
disp('x1 x2');
[x01;x02]
disp('Hk')
subs(subs(A,x1,x01),x2,x02)
disp('gamm es');
gamma*eye(size(subs(subs(A,x1,x01),x2,x02)))
disp('diff');
(subs(subs(B,x1,x01),x2,x02))
disp('------------');
F_eval_old=subs(subs(a,x1,x01),x2,x02);
x01=temp(1,1);
x02=temp(1,2);
F_eval_new=subs(subs(a,x1,x01),x2,x02);
if F_eval_new<F_eval_old
gamma=.5*gamma;
else
gamma=2*gamma;
end
mag_of_B=sqrt(subs(subs(diff(a,x1),x1,x01),x2,x02)^2+subs(subs(diff(a,x2),x1,x01),x2,x02)^2)
records=[records;[k,x01,x02,subs(subs(a,x1,x01),x2,x02)]];
end
records
The error might be due to the version I used is 2021b, but i didn't know which part should i changed to clear the error. this is the error of the code:
Error using sym>convertChar (line 1580)
Character vectors and strings in the first argument can only specify a variable or number. To evaluate character vectors and
strings representing symbolic expressions, use 'str2sym'.
Error in sym>tomupad (line 1296)
S = convertChar(x);
Error in sym (line 234)
S.s = tomupad(x);
Error in sym/privResolveArgs (line 1016)
argout{k} = sym(arg);
Error in sym/diff (line 22)
args = privResolveArgs(S,varargin{:});
Error in ww (line 25)
A=[diff(diff(a,x1),x1),diff(diff(a,x1),x2);...
0 commentaires
Réponse acceptée
Shivang
le 5 Sep 2023
Hi Tan,
I understand you're running into an error regarding the use of symbolic expressions in your code.
Note that you cannot directly define symbolic expressions as a string or character vector. The following line in your code causes the above error:
a='(x1^2+x2-11)^2+(x1+x2^2-7)^2';
To evaluate a string representing a symbolic expression, use the 'str2sym' function. Replace the above line in your code with:
a= str2sym('(x1^2+x2-11)^2+(x1+x2^2-7)^2');
You should no longer see any symbolic expression related errors in your code.
Hope this helps.
-Shivang
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Number Theory dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!