how to plot x= 0:0.01:10

53 views (last 30 days)
Bastian
Bastian on 10 Dec 2023
Answered: Image Analyst on 10 Dec 2023
i attempted to do this but instead of giving me 0,1,2,3,4,5,6,7,8,9,10 on the x axis it gives me 0,5,15,20 idont understand why. Do i have to use linspace instead, on the newer version of matlab instead of what i used in the question?

Accepted Answer

Dyuman Joshi
Dyuman Joshi on 10 Dec 2023
I'm not sure what are you plotting against, but you will have to specify xticks manually -
x = 0:0.01:10;
y = sin(x);
%Default plot
figure
plot(x, y)
%Modified xticks
figure
plot(x,y)
xticks(0:10)

More Answers (1)

Image Analyst
Image Analyst on 10 Dec 2023
Your badly-named "x" variable (almost everyone else would have called it y) goes from 0 to 10 in a thousand and one steps. When you plot just x, it will use x as the y value and the index (1 to 1001) as the x value.. Hence you will get a plot like this.
x = 0:0.01:10
x = 1×1001
0 0.0100 0.0200 0.0300 0.0400 0.0500 0.0600 0.0700 0.0800 0.0900 0.1000 0.1100 0.1200 0.1300 0.1400 0.1500 0.1600 0.1700 0.1800 0.1900 0.2000 0.2100 0.2200 0.2300 0.2400 0.2500 0.2600 0.2700 0.2800 0.2900
plot(x, 'b.-', LineWidth=2);
grid on;
xlabel('Index');
ylabel('Value of x');
title('x = 0:0.01:10')
Note that the x axis plots indexes 1 - 1001, and the y axis plots the value of your badly-named data.
If you have some other y and you want your x to actually be x, then you can't just plot(x), you must plot(x, y). Then to set up your x tick mark labels you can use xticks
xticks(0 : 10);

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!