Plotting a Function y(x) in MATLAB

In summary, the conversation discusses the task of plotting a function in MATLAB with specific conditions for the values of x and y. The initial code provided only plots the final case and it becomes tedious to plot each point separately. Alternative solutions are suggested, such as using a for loop and if statements to plot the function. However, there is a potential issue with the definition of the function and a revised definition is proposed to address this.
  • #1
sara_87
763
0

Homework Statement



I want to plot into MATLAB the following function y(x):

when x is between 1 and 2: y=1
when x is between 2 and 3: y=2
.
.
.
when x is between 9 and 10: y=9
etc

I have tried a code. but this code only plots the curve for the final case (so y=9 when x is between 9 and 10).

I know that i can join 2 points like this:
eg
plot([1 2],[1 1],[2 3],[2 2])
However, it will be tedious if i was to do this 10 times (or even more).

Homework Equations





The Attempt at a Solution



I have tried the following code:

for i =1:10
x(i) = i
end
for j=1:9
if j==i || j==i+1
y(j)=i
end
plot([x(j) x(j+1)],[y(j) y(j)])
end


Any help or ideas will be very much appreciated.
Thank you
 
Physics news on Phys.org
  • #2
sara_87 said:

Homework Statement



I want to plot into MATLAB the following function y(x):

when x is between 1 and 2: y=1
when x is between 2 and 3: y=2
.
.
.
when x is between 9 and 10: y=9
etc

I have tried a code. but this code only plots the curve for the final case (so y=9 when x is between 9 and 10).

I know that i can join 2 points like this:
eg
plot([1 2],[1 1],[2 3],[2 2])
However, it will be tedious if i was to do this 10 times (or even more).

Homework Equations





The Attempt at a Solution



I have tried the following code:

for i =1:10
x(i) = i
end
for j=1:9
if j==i || j==i+1
y(j)=i
end
plot([x(j) x(j+1)],[y(j) y(j)])
end


Any help or ideas will be very much appreciated.
Thank you

Hi sarah_87,
There's a problem with this definition:
when x is between 1 and 2: y=1
when x is between 2 and 3: y=2
.
.
.
when x is between 9 and 10: y=9
What happens when x equals one of the endpoints, say x = 2?

The first line sets y to 1, but the next line sets y to 2.

I'm going to assume that this is what you mean:
when x >= 1 && x < 2, y=1
when x >= 2 && x < 3, y=2
.
.
when x >= 9 && x < 10, y=9

See if this does what you want.
Code:
for i =1:10
x(i) = i
end

for j = 1:9
  for i = 1:10
    if j >= i && j < (i + 1)
       y(j)=i
    end
  end
end
plot(x, y)
%%plot([x(j) x(j+1)],[y(j) y(j)])
end
 

1. How do I plot a function y(x) in MATLAB?

To plot a function y(x) in MATLAB, you can use the plot function. This function takes in two vectors as inputs - one for the x-values and one for the y-values. For example, if your function is y = x^2, you can create a vector of x-values using the linspace function and then calculate the corresponding y-values. Then, use plot(x,y) to plot the function.

2. Can I plot multiple functions on the same graph in MATLAB?

Yes, you can plot multiple functions on the same graph in MATLAB by using the hold on command. This allows you to plot multiple functions without erasing the previous ones. Just make sure to use hold off when you are done plotting all your functions.

3. How can I customize my plot in MATLAB?

There are many ways to customize your plot in MATLAB. You can change the color, style, and size of the lines using the color, linestyle, and linewidth parameters in the plot function. You can also add a title, axis labels, and a legend using the title, xlabel, ylabel, and legend functions.

4. What is the difference between plotting a function and plotting data points in MATLAB?

When plotting a function, MATLAB calculates the y-values for each x-value based on the function's equation. However, when plotting data points, you provide the x and y values directly. Additionally, when plotting data points, you can use the scatter function to create a scatter plot instead of a line plot.

5. How can I save my plot as an image in MATLAB?

You can save your plot as an image in MATLAB by using the saveas function. This function takes in the figure handle and the desired file name and file format as inputs. For example, saveas(gcf, 'my_plot.png') will save the current figure as a PNG file in your current working directory.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
881
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
826
  • Engineering and Comp Sci Homework Help
Replies
7
Views
886
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
855
  • Engineering and Comp Sci Homework Help
Replies
3
Views
807
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
Back
Top