Looking for the correct way to go about a Differential Equation in Matlab

1 view (last 30 days)
The question I'm having problems with calls for an undamped pendulum that is defined by:
theta" + sin(theta) = 0. theta(0) = 0, theta'(0) = b.
and for this x = theta and y = theta'; with x and y satisfying the system
x' =y x(0) = 0
y' = -sin(x) y(0) = b
And I am supposed to solve the system numerically with b equal to a range of numbers from 0.5 to 2.5
This is the code I have currently written for the problem, but I can tell it's most likely wrong because of the graph that I get:
figure
hold on
ode1 = diff(x1) == -2*x1 + x2;
ode2 = diff(x2) == 3*x1 - 2*x2;
odes = [ode1; ode2];
cond1 = x1(0) == 0;
for i = [0.5 1 1.5 2 2.5]
cond2 = x2(0) == i;
conds = [cond1; cond2];
[S1, S2] = dsolve(odes,conds);
fplot(S1)
fplot(S2)
end
xlabel 'x'
ylabel 'y'
So if anyone could point to me where I went wrong in my code or how I should be approaching the question it would be greatly appreciated!
I will also still be looking for the correct way to approach this question and update if I found the correct way!

Answers (1)

Sam McDonald
Sam McDonald on 19 Apr 2017
If you want to solve this numerically, you will want to use a numerical solver like "ode45" instead of a symbolic solver like "dsolve".
Useful examples for quickly setting up and solving differential equations are found in the MATLAB Documentation for solving Ordinary Differential Equations. The function "ode45" accepts a function of the form y' = f(t,y) as an input to solve the equation. Use the examples to help you get started:

Community Treasure Hunt

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

Start Hunting!