Using eval undefined function or variable
2 views (last 30 days)
Show older comments
Rostistlav Stoyanov
on 7 Jan 2018
Answered: Walter Roberson
on 7 Jan 2018
So I have a exercise to solve a differential equation and I have to use Pikar's method to draw some of the approximations.However I have trouble using eval. Here is my code:
function ex1
hold on;
grid on;
xmin=1;xmax=3;
x0=2;y0=2;
axis([xmin xmax -1,5]);
y=dsolve('Dy=x*(3*y-2)*sin(1-4*x^2)','y(2)=2','x')
t=xmin:(xmax-xmin)/100:xmax;
plot(t,eval(y),'k');
function z = ff(x,y)
z=x.*(3*y-2).*sin(1-4*(x.^2));
end
% Calculating the approximations
X=xmin:(xmax-xmin)/100:xmax;
Y=y0*ones(1,length(X));
plot(X,Y,'y');
Z=Y;
N=8;
for k=1:N
YK=y0+cumtrapz(X,ff(X,Z));
if(k==1)
plot(X,Y,'g');
elseif(k==2)
plot(X,Z,'b');
elseif(k==7)
plot(X,Z,'r');
end
Z=YK;
end
end
And the errors I am getting are :
Error using evalin
Undefined function or variable 'x'.
Error in sym/eval (line 11)
s = evalin('caller',vectorize(map2mat(char(x))));
Error in ex1 (line 12)
plot(t,eval(y),'k');
0 Comments
Accepted Answer
Walter Roberson
on 7 Jan 2018
Do not eval() a symbolic expression. Symbolic expressions are in a language that is not MATLAB and is not MUPAD.
The result of the dsolve is the symbolic expression
(4*exp(-(3*cos(15))/8)*exp((3*cos(4*x^2 - 1))/8))/3 + 2/3
When you try to eval() that, you need to have a variable named x defined, but you do not.
sol = double( subs(y, sym('x'), t) );
plot(t, sol)
0 Comments
More Answers (0)
See Also
Categories
Find more on Symbolic Math Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!