How to get values at different interval for ode45

7 views (last 30 days)
Hello,
I solved a second order differential equation with ode45, the code ran smoothly.
I want to extract the value of the results at different intervals 0:0.1:1.
Is there a way to this in Matlab?
function Shooting_Method
clc;clf;clear;
global C__T R__D S__h Q gamma beta P__e
C__T=0.1;
R__D=0.3;
S__h = 0.1;
Q = 0.4;
gamma = 0.1;
beta = 0.2;
P__e = 1;
x=0.5;
x1=fzero(@solver,x)
end
function F=solver(x)
options=odeset('RelTol', 1e-9, 'AbsTol', [1e-9 1e-9], 'OutputFcn', @odeplot);
[t,u] = ode45(@equation, [0 1], [x,0], options);
s=length(t);
F=u(s,1)-1;
figure(1)
plot(t,u(:,1))
end
function dy = equation(t,y)
global C__T R__D S__h Q gamma beta P__e
dy=zeros(2,1);
dy(1) = y(2);
dy(2) = ((4 * C__T ^ 3 * y(1) * R__D) + (6 * C__T ^ 2 * R__D * y(1) ^ 2)+...
(4 * C__T * R__D * y(1) ^ 3) + (y(1) ^ 4 * R__D) - S__h * Q * gamma...
* y(1) - beta * y(2) ^ 2 + S__h * (y(1) ^ 2) + P__e * y(2) - S__h * Q)...
/ (beta * y(1) + 0.1e1);
end

Answers (1)

darova
darova on 18 Sep 2019
Use for loop to get results for x = 0: 0.1: 1 (11 curves)
hold on
for x = 0:0.1:1
[t,u] = ode45(@equation, [0 1], [x 0])
plot(t,u(:,1))
end
hold off
  2 Comments
Mayokun  Ojediran
Mayokun Ojediran on 18 Sep 2019
@darova I am not trying to solve for different values of x, I am trying to display the results for u for different values of t. Thank you
darova
darova on 18 Sep 2019
There two ways to do this
First one
tspan = 0:0.1:1;
[t,u] = ode45(@equation, tspan, [x 0])
plot(t,u(:,1))
Second one
[t,u] = ode45(@equation, [0 1], [x 0])
t1 = 0:0.1:1;
u1 = interp1(t,u(:,1),t1);
plot(t,u(:,1)) % ode45 data
hold on
plot(t1,u1,'.r') % data at specific points
hold off

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Tags

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!