how do i write sin^2(x) in matlab?
325 次查看(过去 30 天)
显示 更早的评论
I am trying to plot sin^2(x) together with cos^2(x) between [0,2pi]
but cant get my matlab to accept sin^2(x).
here is what I wrote, what am i doing wrong?
x=0:0.01:2*pi
si=sin^2(x);
co=cos^2(x);
plot(si,co)
5 个评论
Steven Lord
2024-1-23
That is the correct value for the sine of -5 radians. If you wanted to compute the sine of -5 degrees use the sind function instead of the sin function.
R1 = -5; % radians
D1 = rad2deg(R1) % -5 radians in degrees
sineOfMinus5Radians = [sin(R1); sind(D1)]
D2 = -5; % degrees
R2 = deg2rad(D2) % -5 degrees in radians
sineOfMinus5Degrees = [sind(D2); sin(R2)]
Dyuman Joshi
2024-1-23
编辑:Dyuman Joshi
2024-1-23
@Hammad, the input of sin() is considered as radian, which can be seen from the documentation of sin (easily foundable by a simple search on the internet), thus the value is provided accordingly.
Also note that the output shown is a truncated value upto 4 digits after decimal, which is not what the actual value is.
采纳的回答
David Hill
2020-9-4
编辑:David Hill
2020-9-4
x=0:0.01:2*pi;
si=sin(x).^2;
co=cos(x).^2;
plot(x,si,x,co);
figure;
plot(si,co);%not sure which one you want
4 个评论
Image Analyst
2022-3-24
An alternative to specifying the spacing is to specify the number of elements in the vector with linspace(), like
numElements = 2000; % Should be enough to fit all the way across your screen.
x = linspace(0, pi, numElements);
y = sin(2 * x);
plot(x, y, 'b-', 'LineWidth', 2);
grid on;
xlabel('x');
ylabel('y');
title('Y vs X');
xlim([min(x), max(x)]);
更多回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!