![](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!