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.
0 个评论
采纳的回答
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 个评论
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 Exchange 中查找有关 Calculus 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!