f(x) = (sin(x))^0.5
4 views (last 30 days)
Show older comments
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)
0 Comments
Accepted Answer
Geoff Hayes
on 4 Apr 2015
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 Comments
More Answers (0)
See Also
Categories
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!