ode45 is not generating the proper graph for this 1st order diff. eqn.

For this problem, I am given the equation:
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.
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:
I'd like to know what exactly I've done wrong with my code here.

 Accepted Answer

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,'--')

2 Comments

Oh wow, I never even thought about the time interval limiting my solution. Thank you!
Is it possible to do [-1 10] for the time interval or do you have to do [0 10] and [0 -1] and merge the graphs together?
It is possible, but since you provided initial condition at , I had to resort to going backward in time.
If you know , then you can use interval in straightforward fashion: time=[ -1 10]

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products

Release

R2021a

Community Treasure Hunt

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

Start Hunting!