Solve/Plot Second Order DiffEq w/ Two Inital Conditions
์กฐํ ์: 3 (์ต๊ทผ 30์ผ)
์ด์ ๋๊ธ ํ์
Erin Hayes
2022๋
2์ 3์ผ
๋๊ธ: William Rose
2022๋
2์ 3์ผ
I have to solve and plot the result of a second order differential equation using ode45 in MATLAB. I have used ode45 before and cannot figure out how to put a second inital condition in my statements when using a simple code like this (an example of code I have used before):
t2span = [5 10];
v02 = 0; %one initial condition
u2=0; %constant
[t2,v2] = ode45(@(t2,v2) u2-abs(v2)*v2, t2span, v02); %simple ode45 statement
This is the equation given: ๐ฅฬ =6sin(๐ก)โ๐ฅ^5 โ0.6๐ฅฬ , and I must plot x(t) from t=0 to t=240seconds, with initial conditions x(0)=2, ๐ฅฬ(0)=3.
This is what I have, it works but when I try to do the second part of the problem it makes me think it is incorrect (the second part makes me split the diffeq into its three terms and solve them and then plot them on a combined graph).
M=@(t,X)[X(2);6*sin(t)-X(1).^5-0.6*X(2)] %creating a function
sol = ode45(M,[0 240],[2 3]); %solving using ode45
fplot(@(x)deval(sol,x,1), [0, 240]) %plotting
Please let me know if this is correct or if there is an easier way to do this because I am not sure if this is correct. I got this code from looking up information on ode45 and other ways of solving diffeqs in MATLAB.
๋๊ธ ์: 0
์ฑํ๋ ๋ต๋ณ
William Rose
2022๋
2์ 3์ผ
Your code is good! Instead of using sol on line 2, and instead of fplot() on line 3, you could do as follows. The form below is easier for me to read and understand. I guess that is because it is what I am used to.
Vector t has the time values of the solution. Array x has two columns: [x1,x2]=[x,dx/dt].
M=@(t,X)[X(2);6*sin(t)-X(1).^5-0.6*X(2)]; %create a function
[t,x] = ode45(M,[0 240],[2 3]); %solve using ode45
plot(t,x(:,1),'-b'); %plot results
xlabel('Time'); ylabel('x(t)'); grid on
Same result as with your version, of course.
๋๊ธ ์: 5
์ถ๊ฐ ๋ต๋ณ (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!



