Question

MATLAB! Evaluate the function y=(x-(1/x))/(1+(1/x^2)) for x varying from -2 to 2 with 0.01 steps. Plot...

MATLAB!

Evaluate the function y=(x-(1/x))/(1+(1/x^2)) for x varying from -2 to 2 with 0.01 steps. Plot the function (y versus x) as a line such that it's blue when y<-0.2, red when y<0.2, and green for all the other values.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

% Matlab script to plot the function (y versus x) as a line such that it's blue when y<-0.2, red when y<0.2, and green for all the other values.

% define the x vector
x = (-2:0.01:2);
% calculate the y vector
y=(x-(1./x))./(1+(1./x.^2));

% use find function to get the indexes for y < -0.2 , y >= -0.2 and y < 0.2
% and y >= 0.2
idx1 = find(y < -0.2);
idx2 = find( y >= -0.2 & y < 0.2);
idx3 = find(y>=0.2);

% create a new vectors for x and y to contain all the points to be shown
% with blue line, red line and green line respectively
xBlue = x;
yBlue = y;
xRed = x;
yRed = y;
xGreen = x;
yGreen = y;

% set the points in xBlue which belong to red or green line to NaN so that
% it is not plotted
xBlue(idx3) = NaN;
xBlue(idx2) = NaN;
yBlue(idx3) = NaN;
yBlue(idx2) = NaN;

% Similarly, set the points in xRed which belong to blue or green line to NaN so that
% it is not plotted
xRed(idx3) = NaN;
xRed(idx1) = NaN;
yRed(idx3) = NaN;
yRed(idx1) = NaN;

% Similarly, set the points in xGreen which belong to red or bue line to NaN so that
% it is not plotted
xGreen(idx1) = NaN;
xGreen(idx2) = NaN;
yGreen(idx1) = NaN;
yGreen(idx2) = NaN;

% plot the blue line
plot(xBlue,yBlue,'b-');
hold on; % hold the current graph so that subsequent plot are plotted in the same graph
% plot the red line
plot(xRed,yRed,'r-');
% plot the green line
plot(xGreen,yGreen,'g-');
hold off; % release the current graph
xlabel('X');
ylabel('Y');
% end of script

Output:

Add a comment
Know the answer?
Add Answer to:
MATLAB! Evaluate the function y=(x-(1/x))/(1+(1/x^2)) for x varying from -2 to 2 with 0.01 steps. Plot...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT