필터 지우기
필터 지우기

Plotting Answers to dsolve

조회 수: 6 (최근 30일)
Bob
Bob 2016년 8월 8일
댓글: Star Strider 2016년 8월 8일
Trying to get the answers to show up on plot
Code:
for w = 1.3, 1.5, 1.8, 1.9
dsolve('2*D2h + 8*h = 5*cos(w*t)')
figure; plot(w); axis equal; axis([0 40 0 40])
end
Doing this is giving me a plot but nothing is actually plotted on the graph.

채택된 답변

Star Strider
Star Strider 2016년 8월 8일
Your function may be only a function of time (guessing here because you didn’t specifically state that in your code), however the the integrated function is of both ‘w’ and ‘t’, so if you want to plot it, you have to plot it as a function of both.
This works. I leave it to you to determine if it produces the results you want:
syms h(t) w
Dh(t) = diff(h);
h_sol = dsolve(2*diff(h,2) + 8*h == 5*cos(w*t), h(0) == 0, Dh(0) == 0); % Integrate
h_sol = simplify(h_sol, 'steps', 20); % Simplify
h_fun = matlabFunction(h_sol); % Create Anonymous Function
h_fun = @(t,w)(sin(t.*w.*(1.0./2.0)).^2.*5.0-sin(t).^2.*5.0)./(w.^2-4.0); % Copied & Pasted For Convenience
w = [1.3, 1.5, 1.8, 1.9];
t = linspace(0, 1, 25);
[W,T] = meshgrid(w,t);
figure(1)
meshc(T,W,h_fun(T,W))
grid on
xlabel('\bft')
ylabel('\bfw')
zlabel('\bfh(t,w)')
The meshgrid call is necessary if you want to calculate and plot it correctly.
  댓글 수: 3
Bob
Bob 2016년 8월 8일
This is my updated code:
for w = 1.3, 1.5, 1.8, 1.9
f = dsolve('2*D2h + 8*h = 5*cos(w*t)');
t = 0:1:40;
figure; plot(t,f)
end
But it is saying non numeric data is not supported.
Star Strider
Star Strider 2016년 8월 8일
You weren’t specific about what you wanted. My symbolic code provides the correct solution (conventionally assuming zero initial conditions), and produces an anonymous function for the solution, so I won’t repeat it here.
If you want a family of curves, change the plotting part of the code to:
h_fun = @(t,w)(sin(t.*w.*(1.0./2.0)).^2.*5.0-sin(t).^2.*5.0)./(w.^2-4.0); % Copied & Pasted For Convenience
w = [1.3, 1.5, 1.8, 1.9];
t = linspace(0, 40, 250);
[W,T] = meshgrid(w,t);
figure(1)
plot(t, h_fun(T,W))
grid on
xlabel('\bft')
ylabel('\bfh(t,w)')
lgndcell = regexp(sprintf('w = %.1f\n',w), '\n','split');
legend(lgndcell(1:end-1), 'Location','NW')
That should do everything you want:

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Calculus에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by