Plotting a Function y(x) in MATLAB

Click For Summary
SUMMARY

The discussion focuses on plotting a piecewise function in MATLAB, specifically defining y(x) for intervals between 1 and 10. The original attempt by the user resulted in only plotting the last segment of the function. A corrected approach was provided, using nested loops to assign values to y based on the defined intervals, ensuring proper handling of endpoint conditions. The final solution utilizes a single plot command to visualize the entire function effectively.

PREREQUISITES
  • Understanding of MATLAB syntax and functions
  • Familiarity with piecewise functions
  • Knowledge of control flow structures in programming (loops and conditionals)
  • Basic plotting techniques in MATLAB
NEXT STEPS
  • Explore MATLAB's plotting functions, specifically the 'plot' function
  • Learn about piecewise function implementation in MATLAB
  • Investigate MATLAB's logical indexing for efficient data manipulation
  • Study control flow structures in MATLAB to enhance programming skills
USEFUL FOR

This discussion is beneficial for MATLAB users, particularly students and educators working on mathematical modeling, programmers seeking to improve their plotting skills, and anyone interested in visualizing piecewise functions in MATLAB.

sara_87
Messages
748
Reaction score
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
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
 

Similar threads

  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
2
Views
2K
Replies
4
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
Replies
7
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K