Plotting Answers to dsolve

8 ビュー (過去 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 Center および File ExchangeTitle についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by