The issue you are facing is likely due to variable scope in your code. The updateSystem function is unable to access the “Base” workspace variables leading to this error. To resolve this, you may encapsulate your entire code into a function and make relevant modifications to the odefun function handle. Here is a modified code snippet for your reference:
function ODEvisualize
f = figure('Name', 'ODE Parameter Variation', 'NumberTitle', 'off', ...
'Position', [100, 100, 600, 400]);
% Create a slider for parameter 'a'
uicontrol('Style', 'text', 'Position', [50, 340, 100, 20], 'String', 'Parameter a:');
a_slider = uicontrol('Style', 'slider', 'Min', 0, 'Max', 10, 'Value', 0.5, ...
'Position', [150, 340, 300, 20], ...
'Callback', @updatePlot);
ax = axes('Units', 'pixels', 'Position', [50, 50, 500, 250]);
% Define the differential equations
phi = @(x) (1/16) * x - (1/6) * x;
f1 = @(x, y, z, a) a * (y - phi(x));
f2 = @(x, y, z) x - y + z;
f3 = @(x, y, z, b) -b * y;
% Create a function handle for the ODEs
odefun = @(t, X, a, b) [f1(X(1), X(2), X(3), a); f2(X(1), X(2), X(3)); f3(X(1), X(2), X(3), b)];
updatePlot();
function updatePlot(~, ~)
% Get the current value of 'a' from the slider
a = get(a_slider, 'Value');
b = 14; % Fixed value of b
% Solve the ODE
tspan = [0, 10];
X0 = [1, 1, 1]; % Initial conditions
[t, X] = ode45(@(t, X) odefun(t, X, a, b), tspan, X0);
% Plot the results
plot(ax, t, X(:, 1), 'r', t, X(:, 2), 'g', t, X(:, 3), 'b');
xlabel(ax, 'Time');
ylabel(ax, 'Solution');
legend(ax, {'x(t)', 'y(t)', 'z(t)'});
title(ax, sprintf('ODE Solution with a = %.2f', a));
grid(ax, 'on');
end
end
Output:
Please find the relevant documentation for your reference.
I hope this resolves the errors you are facing!