Solve/Plot Second Order DiffEq w/ Two Inital Conditions
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Erin Hayes
am 3 Feb. 2022
Kommentiert: William Rose
am 3 Feb. 2022
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 Kommentare
Akzeptierte Antwort
William Rose
am 3 Feb. 2022
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 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Ordinary Differential Equations finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



