Error using sym/matlabFunction>getOptions “Invalid Values”
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.
close all; clc; clear;
% Constants
t0 = 1; % initial time
tf = 2; % final time
stepSize = 0.5; % step size for numerical methods
syms t y(t) % Define symbolic variables
% MATLAB Symbolic Math Toolbox
% Define the ODE
ode = t^2*diff(y,t,2) – 2*t*diff(y,t) + 2*y == 0;
% THE ISSUE ARISES HERE
odeFunction = matlabFunction(rhs(ode), ‘Vars’, {t, y});
% Solve the ODE symbolically
sol = dsolve(ode);
% Display the solution
disp(sol);
% Euler’s Method
[t_euler, y_euler] = Euler(odeFunction, 4, tf, stepSize);
fprintf(‘n’);
% Improved Euler’s Method
[t_improved, y_improved] = EulersImproved(odeFunction, 4, tf, stepSize);
% Euler’s function implementation.
function [t, y] = Euler(odeFunction, initialValue, targetValue, stepSize)
% Initialize arrays to store time and solution values
t = 1:stepSize:targetValue;
y = zeros(size(t));
% Set initial value
y(1) = initialValue;
% Euler method to solve the ODE
for i = 1:length(t) – 1
% Compute the next value using Euler’s method
y(i + 1) = y(i) + stepSize * odeFunction(t(i), y(i));
end
% Display the final result at the target value
fprintf(‘The value of the equation, using Eulers Method, at x = %.2f is %.4fn’, targetValue, y(end));
end
% Improved Euler’s function implementation.
function [t, y] = EulersImproved(odeFunction, initialValue, targetValue, stepSize)
% Initialize arrays to store time and solution values
t = 1:stepSize:targetValue;
y = zeros(size(t));
% Set initial value
y(1) = initialValue;
% Improved Euler’s method to solve the ODE
for i = 1:length(t) – 1
% Predictor step
y_pred = y(i) + stepSize * odeFunction(t(i), y(i));
% Corrector step
y(i + 1) = y(i) + 0.5 * stepSize * (odeFunction(t(i), y(i)) + odeFunction(t(i + 1), y_pred));
end
% Display the final result at the target value
fprintf(‘The value of the equation, using the Improved Eulers Method, at x = %.2f is %.4fn’, targetValue, y(end));
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!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.
close all; clc; clear;
% Constants
t0 = 1; % initial time
tf = 2; % final time
stepSize = 0.5; % step size for numerical methods
syms t y(t) % Define symbolic variables
% MATLAB Symbolic Math Toolbox
% Define the ODE
ode = t^2*diff(y,t,2) – 2*t*diff(y,t) + 2*y == 0;
% THE ISSUE ARISES HERE
odeFunction = matlabFunction(rhs(ode), ‘Vars’, {t, y});
% Solve the ODE symbolically
sol = dsolve(ode);
% Display the solution
disp(sol);
% Euler’s Method
[t_euler, y_euler] = Euler(odeFunction, 4, tf, stepSize);
fprintf(‘n’);
% Improved Euler’s Method
[t_improved, y_improved] = EulersImproved(odeFunction, 4, tf, stepSize);
% Euler’s function implementation.
function [t, y] = Euler(odeFunction, initialValue, targetValue, stepSize)
% Initialize arrays to store time and solution values
t = 1:stepSize:targetValue;
y = zeros(size(t));
% Set initial value
y(1) = initialValue;
% Euler method to solve the ODE
for i = 1:length(t) – 1
% Compute the next value using Euler’s method
y(i + 1) = y(i) + stepSize * odeFunction(t(i), y(i));
end
% Display the final result at the target value
fprintf(‘The value of the equation, using Eulers Method, at x = %.2f is %.4fn’, targetValue, y(end));
end
% Improved Euler’s function implementation.
function [t, y] = EulersImproved(odeFunction, initialValue, targetValue, stepSize)
% Initialize arrays to store time and solution values
t = 1:stepSize:targetValue;
y = zeros(size(t));
% Set initial value
y(1) = initialValue;
% Improved Euler’s method to solve the ODE
for i = 1:length(t) – 1
% Predictor step
y_pred = y(i) + stepSize * odeFunction(t(i), y(i));
% Corrector step
y(i + 1) = y(i) + 0.5 * stepSize * (odeFunction(t(i), y(i)) + odeFunction(t(i + 1), y_pred));
end
% Display the final result at the target value
fprintf(‘The value of the equation, using the Improved Eulers Method, at x = %.2f is %.4fn’, targetValue, y(end));
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! 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.
close all; clc; clear;
% Constants
t0 = 1; % initial time
tf = 2; % final time
stepSize = 0.5; % step size for numerical methods
syms t y(t) % Define symbolic variables
% MATLAB Symbolic Math Toolbox
% Define the ODE
ode = t^2*diff(y,t,2) – 2*t*diff(y,t) + 2*y == 0;
% THE ISSUE ARISES HERE
odeFunction = matlabFunction(rhs(ode), ‘Vars’, {t, y});
% Solve the ODE symbolically
sol = dsolve(ode);
% Display the solution
disp(sol);
% Euler’s Method
[t_euler, y_euler] = Euler(odeFunction, 4, tf, stepSize);
fprintf(‘n’);
% Improved Euler’s Method
[t_improved, y_improved] = EulersImproved(odeFunction, 4, tf, stepSize);
% Euler’s function implementation.
function [t, y] = Euler(odeFunction, initialValue, targetValue, stepSize)
% Initialize arrays to store time and solution values
t = 1:stepSize:targetValue;
y = zeros(size(t));
% Set initial value
y(1) = initialValue;
% Euler method to solve the ODE
for i = 1:length(t) – 1
% Compute the next value using Euler’s method
y(i + 1) = y(i) + stepSize * odeFunction(t(i), y(i));
end
% Display the final result at the target value
fprintf(‘The value of the equation, using Eulers Method, at x = %.2f is %.4fn’, targetValue, y(end));
end
% Improved Euler’s function implementation.
function [t, y] = EulersImproved(odeFunction, initialValue, targetValue, stepSize)
% Initialize arrays to store time and solution values
t = 1:stepSize:targetValue;
y = zeros(size(t));
% Set initial value
y(1) = initialValue;
% Improved Euler’s method to solve the ODE
for i = 1:length(t) – 1
% Predictor step
y_pred = y(i) + stepSize * odeFunction(t(i), y(i));
% Corrector step
y(i + 1) = y(i) + 0.5 * stepSize * (odeFunction(t(i), y(i)) + odeFunction(t(i + 1), y_pred));
end
% Display the final result at the target value
fprintf(‘The value of the equation, using the Improved Eulers Method, at x = %.2f is %.4fn’, targetValue, y(end));
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! vars, error MATLAB Answers — New Questions