unable to convert sym value to double

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 Comments

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
vpax1 = 
vpay1 = 
1 3.0000 5.0000 -2.6712 6.3009 0.0001
vpax1 = 
vpay1 = 
2
Error using fprintf
Conversion to double from sym is not possible.
.
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
1 3.0000 5.0000 -2.6712 6.3009 0.0001 2 2.3042 3.5485 1.2242 0.7593 0.0001 3 3.7763 1.4693 2.3857 2.0659 0.0001 4 20.5512 -2.2059 -19.2308 -11.9802 0.0001 5 9.1136 -3.3946 0.7417 -11.5549 0.0001 6 4.5388 -5.1777 8.6561 1.6420 0.0001 7 1.7730 -8.7064 -10.1959 -2.5835 0.0001 8 -6.4568 -62.6676 9.2683 55.8558 0.0001 9 -11.4787 -13.9977 -1.5287 5.4063 0.0001 10 -11.7530 -13.2566 -2.6494 0.5449 0.0001 11 -16.3129 -8.2441 9.8985 -0.0771 0.0001 12 -19.1719 -7.2335 17.3906 -3.7856 0.0001 13 -17.1013 -7.1028 5.0074 -12.4262 0.0001 14 -19.7343 -6.7631 13.8453 -12.7202 0.0001 15 -17.4105 -6.6091 -1.4779 -15.1260 0.0001 16 -20.2326 -6.4881 9.9908 -18.0983 0.0001 17 -17.5951 -6.1870 -8.0697 -15.0891 0.0001 18 -20.7242 -6.3288 6.4830 -22.0960 0.0001 19 -17.5322 -5.7571 -14.8776 -13.2176 0.0001 20 -21.7371 -6.5264 6.3753 -26.9143 0.0001 21 -1.5441 -0.4805 0.6941 -0.8565 0.0001 22 -0.8359 -0.3295 0.0150 -0.0699 0.0001 23 -0.7840 -0.3382 -0.0011 -0.0001 0.0001
Iteration Count = 23
The X Root = -0.78439
The Y Root = -0.33879

Sign in to comment.

Answers (0)

Categories

Products

Release

R2018a

Asked:

on 7 May 2022

Edited:

on 7 May 2022

Community Treasure Hunt

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

Start Hunting!