draw a function composed of sin and cos

1 次查看(过去 30 天)
i encounter an error when trying ti plot this function
x=-pi:0.1*pi:pi;
y1=100*sqrt(2*pi)/sqrt(0.05*pi).*cos(x/2)*(1.-cos(x/2.)*sin(x/2.)*sin(3.*x/2.));
figure
plot(x,y1)

采纳的回答

Star Strider
Star Strider 2022-2-26
Use element-wise operations
y1=100*sqrt(2*pi)/sqrt(0.05*pi).*cos(x/2).*(1.-cos(x/2.).*sin(x/2.).*sin(3.*x/2.));
See Array vs Matrix Operations for details.
x=-pi:0.1*pi:pi;
y1=100*sqrt(2*pi)/sqrt(0.05*pi).*cos(x/2).*(1.-cos(x/2.).*sin(x/2.).*sin(3.*x/2.));
figure
plot(x,y1)
.
  1 个评论
Steven Lord
Steven Lord 2022-2-26
What @Star Strider has written is correct but one section might be slightly misleading for newer users.
% (1.-cos(x/2.).*sin(x/2.).*sin(3.*x/2.))
That first part may make you think there is an operator .- that does element-wise subtraction. But that's not the case. That period is not part of the operator but part of the number itself, since subtraction already applies element-wise. [There's also no .+ operator; just + is sufficient.]
x = 1.; % one
y = 1; % also one
x == y % true
ans = logical
1
Note that something like this would error instead.
x = 1.1 .- 1
Invalid use of operator.

Error in connector.internal.fevalMatlab

Error in connector.internal.fevalJSON

请先登录,再进行评论。

更多回答(0 个)

类别

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

标签

Community Treasure Hunt

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

Start Hunting!

Translated by