Plotting Answers to dsolve

6 次查看(过去 30 天)
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