f(x) = (sin(x))^0.5

4 次查看(过去 30 天)
Austin
Austin 2015-4-4
im trying to plot a graph of y = sqrt(sin(x)) for domain -10<=x<=10 however, im trying to convert my graph from left figure to the figure on the right to exclude all y-values that is non-real. Can anyone advise what must i do to the domain?
below are my attempts:
x2=-10/pi:2pi:10/pi
y=sqrt(sin(x2))
plot(x2,y,'color','blue','linewidth',2)

采纳的回答

Geoff Hayes
Geoff Hayes 2015-4-4
Austin - in your question, you indicate that the domain for x is [-10,10], yet you have
x2=-10/pi:2*pi:10/pi;
which (when corrected for 2*pi) only returns am array of two elements
-3.1831 3.1001
of the domain for [-10/pi, 10/pi]. Instead, use linspace to generate a linearly spaced vector/array of elements for your domain. For example,
x = linspace(-10,10,2000);
will generate 2000 elements that are linearly spaced in the interval [-10,10].
Now you want to only plot those values of y that are real, and ignore those that are complex (with non-zero imaginary components). The latter occur whenever sin(x) is negative. You can ignore these by setting them to NaN as
y = sin(x);
y(y<0) = NaN;
and plot the square root as
plot(x,sqrt(y),'color','blue','linewidth',2);
Try the above and see what happens!

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by