Combine graphs into one?
Show older comments
I am solving a dampened spring problem, and am asked to solve the system of ODEs using different values for the coefficient and plot all results in the same graph.
Right now, I have 3 different files (one for each value of coefficient). So, each one has a slightly different system of ODEs.
I have used ode45 to solve them and plot them. But now I am wondering how can I "combine" the 3 plots into one? I seem to remember it's not so hard when you are working on the same file, but here I am not. So, also, is solving the 3 systems on a single file possible?
Here is one of the files for reference:
function overdamped
tspan = 0:0.5:15;
[t,y] = ode45(@f,tspan,[1; 0]);
plot(t,y(:,1))
xlabel('time in s');
ylabel('distance in m');
function dydt = f(~,y)
dydt = zeros(2,1);
dydt(1) = y(2);
dydt(2) = -10*y(2) - y(1);
end
Sol = [t y(:,1)]
end
Thanks
Answers (1)
Jan
on 7 May 2018
tspan = 0:0.5:15;
[t1, y1] = ode45(@f1, tspan, [1, 0]);
[t2, y2] = ode45(@f2, tspan, [1, 0]);
[t3, y3] = ode45(@f3, tspan, [1, 0]);
axes('NextPlot', 'add'); % Equivalent to: hold all
plot(tspan, y1(:,1));
plot(tspan, y2(:,1));
plot(tspan, y3(:,1));
xlabel('time in s');
ylabel('distance in m');
function dydt = f1(~, y)
dydt = [y(2); -10*y(2) - y(1)];
end
function dydt = f2(~, y)
dydt = [y(2); -2*y(2) - y(1)];
end
function dydt = f3(~, y)
dydt = [y(2); 17*y(2) - y(1)];
end
Categories
Find more on Ordinary Differential Equations in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!