Clear Filters
Clear Filters

draw a function composed of sin and cos

1 view (last 30 days)
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)

Accepted Answer

Star Strider
Star Strider on 26 Feb 2022
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 Comment
Steven Lord
Steven Lord on 26 Feb 2022
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

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!