How to plot a simple curve

227 views (last 30 days)
Robert on 4 Feb 2011
Answered: Ademolawa John on 29 Apr 2022
I am trying to learn MATLAB and have stumbled straight away. Can someone please tell me how you plot y = x^2 for x = 1 to 10.
0 CommentsShow -2 older commentsHide -2 older comments

Sign in to comment.

Accepted Answer

the cyclist on 5 Feb 2011
Keeping as close as possible to your notation, to be clear to you:
>> x = 1 : 0.1 : 10;
>> y = x.^2;
>> plot(x,y)
Note that the 0.1 is there to define the intervals. You'll get a smoother curve in your plot if this value is small; the vector will have correspondingly more elements.
I agree with Jiro that you will benefit from a careful read of the documents he references.
1 CommentShow -1 older commentsHide -1 older comments
Robert on 23 Feb 2011
Thank you, this is what I needed, sometimes it can be difficult trawling through documentation when you just want a simple answer. And it was the dot before the power sign that had me lost.

Sign in to comment.

More Answers (7)

Matt Fig on 4 Feb 2011
Or, if you want to be able to do this for a general function (or more):
g = @(x) x.^2; % Create your function for plotting.
h = @(x) x.^2.5; %Create a second function.
x = 1:.01:10; % Create the range for the functions.
plot(x,g(x),'r',x,h(x),'b') % Use a red line for the first, blue for second.
To only plot one function:
plot(x,g(x)) % See help plot for more options.
0 CommentsShow -2 older commentsHide -2 older comments

Sign in to comment.

Jiro Doke on 4 Feb 2011
Edited: John Kelly on 13 Nov 2013
This is a very basic question and we have many places in the documentation for you to learn:
0 CommentsShow -2 older commentsHide -2 older comments

Sign in to comment.

Sean de Wolski on 4 Feb 2011
plot(1:.1:10,1:.1:10.^2)%x = 1 to 10 with spacing of 0.1
Also read the getting started documentation.
%SCd
1 CommentShow -1 older commentsHide -1 older comments
Matt Fig on 4 Feb 2011
Your code will error, Sean de. You are trying to plot different length vectors!

Sign in to comment.

Doug Eastman on 5 Feb 2011
Another useful function for plotting simple expressions is EZPLOT:
ezplot('x^2',[1,10])
or
ezplot(@(x) x.^2,[1,10])
0 CommentsShow -2 older commentsHide -2 older comments

Sign in to comment.

Erick on 11 Sep 2014
hello, how do I change my axes to have different ranges? for my graph below? and I want the curves to run from the x-axis upward to right
2 CommentsShow NoneHide None
the cyclist on 11 Sep 2014
I suggest posting this as a new question, rather than burying as an "answer" to a 3-year-old question.
Yundie Zhang on 17 May 2020
haha,,,,,
set x axix and y axis limits
xlim()
ylim()

Sign in to comment.

le nhat on 18 May 2016
how to paint graph with data activity
1 CommentShow -1 older commentsHide -1 older comments
the cyclist on 18 May 2016
I suggest posting this as a new question, rather than burying as an "answer" to a 5-year-old question.

Sign in to comment.

Ademolawa John on 29 Apr 2022
hello , pls how can I plot this curve that run from top left to bottom right with Y axis running from 1 to 16 and x from 1.8 to 2.7. Thanks
0 CommentsShow -2 older commentsHide -2 older comments

Sign in to comment.

Categories

Find more on Get Started with MATLAB 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!