function not plotting, but no error message

I created this function and plot but it is not plotting and I am not getting any error message.
%x1'=-3x1-200x2
%x2'=200x1-3x2
opts = odeset('RelTol',1e-6,'AbsTol',1e-8);function [dXdt] = Problem02ODEFunction(t,X)
soln = ode45(@Problem02ODEFunction,[0 1],[-10 10],opts);
figure;
plot(soln.x,soln.y(1,:),'-','linewidth',0.5);
hold on;
plot(soln.x,soln.y(2,:),'-','linewidth',0.5);
grid on;
title('Problem 2 ODE45 Solution');
xlabel('Time (s)');
ylabel('Solution');
legend('x_{1}(t)','x_{2}(t)','location','best');
function [dXdt] = Problem02ODEFunction(t,X)
x1 = X(1);
x2 = X(2);
dx1dt = -3*x1 - 200*x2;
dx2dt = 200*x1 - 3*x2;
[dXdt] = [dx1dt; dx2dt];
end

 채택된 답변

Dyuman Joshi
Dyuman Joshi 2023년 9월 27일
The code after the semi-colon in this line of code seems to be a typo
opts = odeset('RelTol',1e-6,'AbsTol',1e-8);function [dXdt] = Problem02ODEFunction(t,X)
After removing it, the code works and the plots are there -
%x1'=-3x1-200x2
%x2'=200x1-3x2
opts = odeset('RelTol',1e-6,'AbsTol',1e-8);
soln = ode45(@Problem02ODEFunction,[0 1],[-10 10],opts)
soln = struct with fields:
solver: 'ode45' extdata: [1×1 struct] x: [0 2.4865e-04 0.0013 0.0023 0.0033 0.0041 0.0048 0.0055 0.0064 0.0073 0.0083 0.0093 0.0103 0.0112 0.0121 0.0127 0.0135 0.0143 0.0153 0.0162 0.0173 0.0183 0.0192 0.0199 … ] (1×1148 double) y: [2×1148 double] stats: [1×1 struct] idata: [1×1 struct]
figure;
plot(soln.x,soln.y(1,:),'-','linewidth',0.5);
hold on;
plot(soln.x,soln.y(2,:),'-','linewidth',0.5);
grid on;
title('Problem 2 ODE45 Solution');
xlabel('Time (s)');
ylabel('Solution');
legend('x_{1}(t)','x_{2}(t)','location','best');
function dXdt = Problem02ODEFunction(t,X)
x1 = X(1);
x2 = X(2);
dx1dt = -3*x1 - 200*x2;
dx2dt = 200*x1 - 3*x2;
[dXdt] = [dx1dt; dx2dt];
end

댓글 수: 2

Jon
Jon 2023년 9월 27일
for this class project they want us doing it in live editor, I removed that semicolon and it still doesn't plot. I shared the whole .mlx file, maybe something else you see is causing a conflict.
No, you don't have to remove the semi colon, you have remove the part after the semi-colon.
I'll take a look at the file and get back at you.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Programming에 대해 자세히 알아보기

제품

릴리스

R2023b

질문:

Jon
2023년 9월 27일

댓글:

2023년 9월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by