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