unable to convert sym value to double
Mostrar comentarios más antiguos
WHEN I RUN THE FOLLOWING CODE I GET THE ERROR THAT MATLAB IS NOT ABLE TO CONVERT SYMBOLIC TO DOUBLE, IT RUNS THE FIRST ITERATION AND THEN STOP DISPLAYING THIS ERROR
f(x,y)=x*sin(y)+y*sin(x)-0.5;
g(x,y)=x*cos(y)-y*cos(x)+0.5;
x0=input('Value for x:');
y0=input('Value for y:');
N = input('Enter number of iterations = ');
tol=0.0001;
f1(x)=diff(f(x,y),x);
f2(y)=diff(f(x,y),y);
g1(x)=diff(g(x,y),x);
g2(y)=diff(g(x,y),y);
for i= 1:N
x1=x0-(((f(x0,y0)*g2(y0))-(g(x0,y0)*f2(y0)))/((f1(x0)*g2(y0))-(f2(y0)*g1(x0))));
y1=y0-(((g(x0,y0)*f1(x0))-(f(x0,y0)*g1(x0)))/((f1(x0)*g2(y0))-(f2(y0)*g1(x0))));
fprintf ('%3i %11.4f %11.4f %11.4f %11.4f %11.4f \n',i,x0,y0,f(x0,y0),g(x0,y0), tol)
if abs(f(x0,y0)) < tol || abs(g(x0,y0)) < tol
fprintf('Iteration Count = %g\n', N)
fprintf('The X Root = %0.5f\n', x1)
fprintf('The Y Root = %0.5f\n', y1)
break
else
x0 = x1;
y0 = y1;
end
if i==N
fprintf('Root not obtained in %1i iterations',N)
end
end
2 comentarios
The ‘x1’ and ‘y1’ values are function of symbolic variables x and y. They need to be evaluated in order to convert them to double variables.
syms x y
sympref('AbbreviateOutput',false);
f(x,y)=x*sin(y)+y*sin(x)-0.5;
g(x,y)=x*cos(y)-y*cos(x)+0.5;
% x0=input('Value for x:');
% y0=input('Value for y:');
% N = input('Enter number of iterations = ');
x0 = 3;
y0 = 5;
N = 2;
tol=0.0001;
f1(x)=diff(f(x,y),x);
f2(y)=diff(f(x,y),y);
g1(x)=diff(g(x,y),x);
g2(y)=diff(g(x,y),y);
for i= 1:N
x1=x0-(((f(x0,y0)*g2(y0))-(g(x0,y0)*f2(y0)))/((f1(x0)*g2(y0))-(f2(y0)*g1(x0))));
y1=y0-(((g(x0,y0)*f1(x0))-(f(x0,y0)*g1(x0)))/((f1(x0)*g2(y0))-(f2(y0)*g1(x0))));
vpax1 = vpa(x1)
vpay1 = vpa(y1)
fprintf ('%3i %11.4f %11.4f %11.4f %11.4f %11.4f \n',i,x0,y0,f(x0,y0),g(x0,y0), tol)
if abs(f(x0,y0)) < tol || abs(g(x0,y0)) < tol
fprintf('Iteration Count = %g\n', N)
fprintf('The X Root = %0.5f\n', x1)
fprintf('The Y Root = %0.5f\n', y1)
break
else
x0 = x1;
y0 = y1;
end
if i==N
fprintf('Root not obtained in %1i iterations',N)
end
end
.
The problem was in assuming that the partial derivatives with respect to one variable would be independent of the other variable.
syms x y
sympref('AbbreviateOutput',false);
f(x,y)=x*sin(y)+y*sin(x)-0.5;
g(x,y)=x*cos(y)-y*cos(x)+0.5;
% x0=input('Value for x:');
% y0=input('Value for y:');
% N = input('Enter number of iterations = ');
x0 = 3;
y0 = 5;
N = 50;
tol=0.0001;
f1(x,y)=diff(f(x,y),x);
f2(x,y)=diff(f(x,y),y);
g1(x,y)=diff(g(x,y),x);
g2(x,y)=diff(g(x,y),y);
for i= 1:N
x1=x0-(((f(x0,y0)*g2(x0,y0))-(g(x0,y0)*f2(x0,y0)))/((f1(x0,y0)*g2(x0,y0))-(f2(x0,y0)*g1(x0,y0))));
y1=y0-(((g(x0,y0)*f1(x0,y0))-(f(x0,y0)*g1(x0,y0)))/((f1(x0,y0)*g2(x0,y0))-(f2(x0,y0)*g1(x0,y0))));
%vpax1 = vpa(x1)
%vpay1 = vpa(y1)
fprintf ('%3i %11.4f %11.4f %11.4f %11.4f %11.4f \n',i,x0,y0,f(x0,y0),g(x0,y0), tol)
if abs(f(x0,y0)) < tol || abs(g(x0,y0)) < tol
fprintf('Iteration Count = %g\n', i)
fprintf('The X Root = %0.5f\n', x1)
fprintf('The Y Root = %0.5f\n', y1)
break
else
x0 = double(x1);
y0 = double(y1);
end
if i==N
fprintf('Root not obtained in %1i iterations',N)
end
end
Respuestas (0)
Categorías
Más información sobre Symbolic Math Toolbox en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



