MATLAB Answers

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

조회 수: 1(최근 30일)
T. Senevirathne
T. Senevirathne 2021년 9월 2일
댓글: Piotr Balik 2021년 9월 3일
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.

채택된 답변

Piotr Balik
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,'--')
  댓글 수: 2
Piotr Balik
Piotr Balik 2021년 9월 3일
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]

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

추가 답변(0개)

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by