![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/728039/image.png)
ode45 is not generating the proper graph for this 1st order diff. eqn.
1 回表示 (過去 30 日間)
古いコメントを表示
For this problem, I am given the equation:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/727884/image.png)
Solving for it by hand, I found the answer to be x(t) = (3/5)*e^(5*t) - (3/5)
From this, I would expect the graph to be an exponential curve that horizontally asymptotes at -(3/5) and cross the origin at (0,0)
However, when I inputted this into MATLAB using ode45, I got a graph that horizontally asymptotes at the x-axis and does not cross through the origin.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/727889/image.png)
Its very possible that I messed up in the code for ode45 but I'm not sure where it occured. Here is the code I used:
clear all
hw0p4func = @(t,x) (5*x)+3;
time = [0 10];
initial = [0 0];
[t,x] = ode45(hw0p4func, time, initial);
plot(t,x(:,1)),title('Problem 4: x vs. time'),xlabel('time'),ylabel('x'),
For reference, this is what the graph should look like:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/727894/image.png)
I'd like to know what exactly I've done wrong with my code here.
0 件のコメント
採用された回答
Piotr Balik
2021 年 9 月 2 日
It is working fine as far as I've tested it:
clear,close all,clc
hw0p4func = @(t,x) (5*x)+3;
initial = [0 0];
time = [0 10];
[t,x] = ode45(hw0p4func, time, initial);
plot(t,x(:,1))
hold on
time = [0 -1]; %backwards in time
[t,x] = ode45(hw0p4func, time, initial);
plot(t,x(:,1)),title('Problem 4: x vs. time'),xlabel('time'),ylabel('x'),
axis([-1 2 -1 10])
You just forgot to compute the negative time part. You can add analytical plot for comparison:
t = -1:0.1:1;
y=(3/5)*exp(5*t) - (3/5);
plot(t,y,'--')
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/728039/image.png)
2 件のコメント
Piotr Balik
2021 年 9 月 3 日
It is possible, but since you provided initial condition at
, I had to resort to going backward in time.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/728364/image.png)
If you know
, then you can use interval in straightforward fashion: time=[ -1 10]
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/728369/image.png)
その他の回答 (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!