Error using sym/matlab​Function>g​etOptions "Invalid Values"

29 views (last 30 days)
Blake
Blake on 10 Apr 2024 at 2:12
Commented: Dyuman Joshi on 19 Apr 2024 at 17:56
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 %.4f\n', 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 %.4f\n', 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!
  2 Comments
Dyuman Joshi
Dyuman Joshi on 10 Apr 2024 at 3:19
% 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});
Why are you converting 0 (rhs of the ode) to a function?
Also, odeFunction is a function in Symbolic Math Toolbox. You should not use functions as names for variables (or scripts for that matter).
And, that function would be a better fit over matlabFunction.

Sign in to comment.

Answers (1)

Star Strider
Star Strider on 10 Apr 2024 at 10:45
Edited: Star Strider on 10 Apr 2024 at 10:45
As noted, the right-hand-side of ‘ode’ is 0, and that is not actually a differential equation. The left-hand-side is.
The missing step is calling the odeToVectorField function that returns the desired result of two first-order differential equations. (Note that you need one initial condition for each differential equation.)
Since this looks like a homework assignment, I leave the rest to you —
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) Y % Define symbolic variables <— CHANGED (Added 'Y')
% 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
[VF,Subs] = odeToVectorField(ode) % <— ADDED
VF = 
Subs = 
odeFunction = matlabFunction(VF, 'Vars', {t, Y}); % — CHANGED (Changed 'Vars' Values)
odeFunction = function_handle with value:
@(t,Y)[Y(2);1.0./t.^2.*(t.*Y(2)-Y(1)).*2.0]
% Solve the ODE symbolically
sol = dsolve(ode);
% Display the solution
disp(sol);
% Euler's Method
[t_euler, y_euler] = Euler(odeFunction, 4, tf, stepSize);
Index exceeds the number of array elements. Index must not exceed 1.

Error in sym/matlabFunction>@(t,Y)[Y(2);1.0./t.^2.*(t.*Y(2)-Y(1)).*2.0]

Error in solution>Euler (line 47)
y(i + 1) = y(i) + stepSize * odeFunction(t(i), y(i));
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 %.4f\n', 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 %.4f\n', targetValue, y(end));
end
.

Tags

Community Treasure Hunt

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

Start Hunting!