Looking for the correct way to go about a Differential Equation in Matlab
4 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
回答(1 个)
Sam McDonald
2017-4-19
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:
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Ordinary Differential Equations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!