Second order differential equation graphing

I need to create this part (only helixs(?))
I created
-------------------------------
syms y(t)
dy = diff(y,t); d2y = diff(y,t,t);
DE = dy2 - 2*dy + 5*y == 0
cond = [y(0) ==4, dy(0) == 0]
sol = dsolve(DE, cond)
y = matlabfunction(sol)
dy1 = matlabfunction(diff(y,t))
x = linspace(-6, 6, 250);
plot(y(x),dy1(x), 'blue', 'LineWidth',3)
----------------------------------------------
This code created
How should I create multiple of lines?
instead of just one line?

 채택된 답변

Stephan
Stephan 2018년 10월 26일
편집: Stephan 2018년 10월 26일

0 개 추천

Hi,
to get different plots you need to change the initial conditions - try this:
clear all
close all
clc
syms y(t) dy(t)
eqn = diff(y,t,2) - 2*diff(y,t) + 5*y == 0;
dy(t) = diff(y,t);
%cond = [y(0) ==4 , dy(0) == 0];
sol_y = dsolve(eqn)
sol_Dy = diff(sol_y,t);
odefun = matlabFunction([sol_y; sol_Dy],'Vars',{'t','C1','C2'});
t = linspace(-6,6,250);
hold on
for k = -4:1:4
res = (odefun(t,k,k/2))';
if mod(k,2) == 0
plot(res(:,1),res(:,2),'b', 'LineWidth',2)
elseif mod(k,2) == 1
plot(res(:,1),res(:,2),'r', 'LineWidth',2)
end
end
xlim([-5 5]);
ylim([-5 5]);
hold off
Usually the commands:
clear all
close all
are not recommended - but since you need C1 and C2 to be named exactly like this i did this, to ensure the constants get correct names. Else they would possibly be named C3 and C4 and the code would not work properly.
If you divide the part of create the function handle from calculate values and plot, you can solve this easily. The first part of the code doesnt need to be computed every time. Just save the function handle to a .m-file and you can use only the second part of the code.
Result is:
.
Best regards
Stephan

추가 답변 (0개)

카테고리

질문:

2018년 10월 25일

편집:

2018년 10월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by