Simulation for a 3D system
조회 수: 6 (최근 30일)
이전 댓글 표시
\begin{align*}
\begin{split}
x^{\prime}=&f_{1} \left (x,y,z \right )=a(y-\phi(x))\\
y^{\prime}=&f_{2} \left (x,y,z \right )=x-y+z \\
z^{\prime}=&f_{3} \left (x,y,z \right )=-b y
\end{split}
\end{align*}
where, $\phi(x)=\frac{1}{16}x-\frac{1}{6}x$.\\
The above system is my system and i want to make a simulation by varying the parameter a and fixitng b to 14 like in this video https://www.youtube.com/watch?v=nQVWD_R--bU . And i have tried the following code but getting errors. How to fix it?
% Define the differential equations
f1 = @(x, y, z, a) a * (y - phi(x));
f2 = @(x, y, z) x - y + z;
f3 = @(x, y, z, b) -b * y;
% Define phi(x)
phi = @(x) (1/16) * x - (1/6) * x;
% Set initial conditions
x0 = 0;
y0 = 0;
z0 = 0;
% Set constant parameter b
b = 14;
% Create a function handle for the ODEs
odefun = @(t, X, a) [f1(X(1), X(2), X(3), a); f2(X(1), X(2), X(3)); f3(X(1), X(2), X(3), b)];
% Time span
tspan = [0 10];
% Create a figure window
f = figure;
% Set initial value of parameter a
a = 0.5;
% Create a slider to control parameter a
b = uicontrol('Parent',f,'Style','slider','Position',[81,54,419,23],...
'value',a, 'min',0, 'max',1);
bgcolor = f.Color;
bl1 = uicontrol('Parent',f,'Style','text','Position',[50,54,23,23],...
'String','0','BackgroundColor',bgcolor);
bl2 = uicontrol('Parent',f,'Style','text','Position',[500,54,23,23],...
'String','1','BackgroundColor',bgcolor);
bl3 = uicontrol('Parent',f,'Style','text','Position',[240,25,100,23],...
'String','Parameter a','BackgroundColor',bgcolor);
% Create a callback function for the slider
b.Callback = @(es,ed) updateSystem(es.Value);
% Function to update the system based on new parameter value of a
function updateSystem(a_value)
% Solve the differential equations
[t, X] = ode45(@(t, X) odefun(t, X, a_value), tspan, [x0; y0; z0]);
% Plot the results
plot(t, X(:, 1), 'r', t, X(:, 2), 'g', t, X(:, 3), 'b');
legend('x', 'y', 'z');
xlabel('Time');
ylabel('Values');
title(['Simulation with a = ', num2str(a_value)]);
end
% Initialize the system with the initial parameter value
updateSystem(a);
I also tried keeping the function in a separate file however still getting errors.
댓글 수: 0
답변 (1개)
Abhinav Aravindan
2024년 10월 11일
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!
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!