Why is the line of y2 always a flat line at x-axis instead of a straight line with negative gradient?

x = linspace(0, 20, 100);
y1 = (exp(x)-1)/10;
y2 = -x+3;
plot(x, y1, 'r');
hold on;
plot(x, y2, 'g');
xlabel('Diode Voltage');
ylabel('Diode Current');
title('Plot of I_D and V_D');

 Accepted Answer

It's flat because of hte scale of your y axis. Look -- it goes from -1 to 50 million, while y2 goes from 3 to -17. How do you expect the line to look when it only changes height of 20 in a scale of 50 million?
If you want to see y2, plot it on a separate axes.

3 Comments

However, I have to show them on the same plot to find their intersecting point. This is why I used hold on. Can I change the limit of y axis?
If this Answer solves your original question, then could you please click the "Accept this answer" link to award the answerer with "reputation points" for their efforts in helping you? They'd appreciate it. Thanks in advance. 🙂 Note: you can only accept one answer (so pick the best one) but you can click the "Vote" icon for as many Answers as you want. Voting for an answer will also award reputation points.

Sign in to comment.

More Answers (1)

x = linspace(0, 20, 100);
y1 = (exp(x)-1)/10;
y2 = -x+3;
%plot(x, y1, 'r');
hold on;
plot(x, y2, 'g');
xlabel('Diode Voltage');
ylabel('Diode Current');
title('Plot of I_D and V_D');
Which is to say that your y1 goes up to about 5 x 10^7. If we were to guess that the plot is about 500 pixels high then that would mean that there would be 5e7/5e2 = 1e5 data units per pixel. Your y2 covers about 20 data units, so on the scale of 0 to 5*10^7 it occupies about 20/1e5 = 0.0002 pixels of y difference. Which is not enough to be visible.

1 Comment

Thanks a lot for your explanation. I did not see the highest value of y-axis and hence could not analyse it before. It worked out now using the function ylim.
Wish you a good day!

Sign in to comment.

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!