I am getting error

1 次查看(过去 30 天)
Cameron Paterson
Cameron Paterson 2021-10-28
编辑: Stephen23 2021-10-28
math error
t=linspace(0,15);
x=(3*t.^2-5)*(t.^3)*sind(t./2)-8*t+27;
v=6*t-2.5*(t.^3)*cosd(t./2)-15*t.^2*sind(t./2)-8;
a=1.25*t^3*sind(t./2)-15*t.^2*cosd(t./2)-30*t*sind(t/2)+6;
plot(t,x,'-',t,v,'--',t,a,'-.','linewidth',1)
title('StreamLine')
xlabel('Time')
ylabel('Velocity')
legend('bestoutside')
grid on

回答(3 个)

Star Strider
Star Strider 2021-10-28
Use element-wise operations everywhere and it works —
t=linspace(0,15);
x=(3*t.^2-5).*(t.^3).*sind(t./2)-8*t+27;
v=6*t-2.5*(t.^3).*cosd(t./2)-15.*t.^2.*sind(t./2)-8;
a=1.25*t.^3.*sind(t./2)-15.*t.^2.*cosd(t./2)-30.*t.*sind(t/2)+6;
figure
plot(t,x,'-',t,v,'--',t,a,'-.','linewidth',1)
title('StreamLine')
xlabel('Time')
ylabel('Velocity')
legend('x','v','a', 'Location','bestoutside')
grid on
The legend call also needs to include the plotted variables, and the names appropriate to the values.
.

Chris
Chris 2021-10-28
In addition to dots in front of carets ( .^ ) and slashes ( ./ ), you need dots in front of the asterisks ( .* ) for elementwise multiplication.
Otherwise, Matlab attempts matrix multiplication.

Stephen23
Stephen23 2021-10-28
编辑:Stephen23 2021-10-28
You need to use array operations, not matrix operations (you missed this for multiplication):
t=linspace(0,15);
x=(3*t.^2-5).*(t.^3).*sind(t./2)-8.*t+27;
% ^ ^ ^
v=6*t-2.5*(t.^3).*cosd(t./2)-15*t.^2.*sind(t./2)-8;
% ^ ^
a=1.25*t.^3.*sind(t./2)-15.*t.^2.*cosd(t./2)-30.*t.*sind(t/2)+6;
% ^ ^ ^ ^ ^ ^
plot(t,x,'-',t,v,'--',t,a,'-.','linewidth',1)
title('StreamLine')
xlabel('Time')
ylabel('Velocity')
legend('Location','bestoutside') % I fixed this for you too
grid on

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by