How do I enter the equation and plot points?

Hello, I'm new to MATLAB and I'm with problems. I need to create a program that scan an equation like y=x+1 and plot 1000 points in the same graphic. I tried creating matrix, usin loops for and while but they didn't work. I create this program that work almost well.
xmin=input('Enter the lowest value of x: ');
xmax=input('Enter the largest value of x: ');
for i=1:1000
X(i)=xmin+(xmax-xmin)*rand(1)
end
[Y]=input('Enter the equation using "X" as the variable: y=');
plot(X,Y)
It works when I enter using some equation like X+1, but when I try another like sin(X) or X.^2 it didn't work like I want. Sorry for my English. Thanks Vinícius

 Accepted Answer

There is no reason this doesn't work. I guess you haven't any error message, just a mixed curve. You can sort you X data to get a desired plot
xmin=input('Enter the lowest value of x: ');
xmax=input('Enter the largest value of x: ');
for i=1:1000
X(i)=xmin+(xmax-xmin)*rand(1);
end
[idx,idx]=sort(X);
y=input('Enter the equation using "X" as the variable: y=');
plot(X(idx),y(idx))

6 Comments

Thank you very much. It solved my problem. I didn't know the command sort and the [idx,idx]. Thanks.
But when I use this, I find a curve and not a lot of points. For example, if i change the "loop for" to -> for i=1:10 i will find a curve and not ten points. And I still didn't solve it yet.
What is the difference between the curve and the points?
I want do something like this for any equation. Maybe I said something wrong.
Ok, in this case use scatter function instead of plot. And it's not necessary to sort your data
xmin=input('Enter the lowest value of x: ');
xmax=input('Enter the largest value of x: ');
for i=1:1000
X(i)=xmin+(xmax-xmin)*rand(1);
end
y=input('Enter the equation using "X" as the variable: y=');
scatter(X,y)
Thank you, I found another way that I can use in the second and in the third dimension. But you helped a lot. Thank you.

Sign in to comment.

More Answers (0)

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!