How do I solve this differential equation 2nd order numerically within matlab?

Hello everyone i am completly new to Matlab and I have a problem with an exercise in my practice book. I have this differential equation:
y(0) = 1 dy(0)=0
and I would like to solve it numerically. I could also solve it simbolically but i already know how to do that and i want to practice.
So first i have to change it in first order differential equation:
But how do i continue? I´m trying for hours at this point😅

 Accepted Answer

There are a couple of points.
(1) As you showed, it is an IVP. But you have one Initial Condition y(0), what about dy(0) =?
(2) What is in sigma(t)?
You can use ode45 - see DOC that shows how to solve similar exercises.
Step 1. Define a fcn file or anonymous fcn handle, e.g.:
F = @(t,y)([y(2); 2*sigma(t) - 3*y(2) - y(1)]);
Step 2. Set up initial conditions:
IC0 = [1; ???];
Step 3. Set up time span to simulate your exercise
t = [0, 10];
Step 4. Solve
[Time, YSol] =ode45(F, time, IC0);
Step 5. Plot the results
plot(Time, YSol(:, 1)), ...

4 Comments

I got sigma(t) through solving it first simbolically. and now have this so far (func4 is the equation broken into 2 first orders :
yo = [1 0];
tspan = 0:.1:10;
[t,y] = ode45 ('func4',tspan,yo);
plot(t,y(:,1))
hold on
plot (t,y(:,2))
hold off
did i make a mistake here? It says the "Index exceeds number of array elements."

Sign in to comment.

More Answers (1)

Here is a corrected complete code:
F = @(t,y)([y(2); 2*sin(t) - 3*y(2) - y(1)]); % Note signa(t) = sin(t)
IC0 = [1; 0]; % Initial conditions
tspan = [0, 10]; % Time span
[t,ysol] = ode45(F,tspan,IC0);
plot(t,ysol(:,1),'b-', 'DisplayName', 'y(t)')
hold on
plot (t,ysol(:,2), 'r-', 'DisplayName', 'dy(t)')
legend show
xlabel('t, [s]')
ylabel('y(t), dy(t)')
grid on
hold off

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!