I'm trying to write a script to solve an ODE using Symbolic Math Toolbox as well as some other numerical solutions. However, when I attempt to convert the solution of the ODE into a function so that I can use it with these numerical methods I encounter an error.
ode = t^2*diff(y,t,2) - 2*t*diff(y,t) + 2*y == 0;
odeFunction = matlabFunction(rhs(ode), 'Vars', {t, y});
[t_euler, y_euler] = Euler(odeFunction, 4, tf, stepSize);
[t_improved, y_improved] = EulersImproved(odeFunction, 4, tf, stepSize);
function [t, y] = Euler(odeFunction, initialValue, targetValue, stepSize)
t = 1:stepSize:targetValue;
y(i + 1) = y(i) + stepSize * odeFunction(t(i), y(i));
fprintf('The value of the equation, using Eulers Method, at x = %.2f is %.4f\n', targetValue, y(end));
function [t, y] = EulersImproved(odeFunction, initialValue, targetValue, stepSize)
t = 1:stepSize:targetValue;
y_pred = y(i) + stepSize * odeFunction(t(i), y(i));
y(i + 1) = y(i) + 0.5 * stepSize * (odeFunction(t(i), y(i)) + odeFunction(t(i + 1), y_pred));
fprintf('The value of the equation, using the Improved Eulers Method, at x = %.2f is %.4f\n', targetValue, y(end));
And it gives the error:
The value of 'Vars' is invalid.
'Vars' value must be a character
vector, a 1-dimensional cell array of
character vectors, a 1-dimensional
cell array of symbolic variables or
arrays of symbolic variables, or an
array of symbolic variables.
Any help would be greatly appreciated!