Plotting a Function Against Its Argument in MATLAB

AI Thread Summary
To plot a function against its argument in MATLAB, you can use the `linspace` function to create a continuous range of x-values. For example, using `x = linspace(0, 5*pi, 50)` generates 50 evenly spaced points between 0 and 5π, allowing for a smooth curve when plotting `y = sin(x)`. While MATLAB requires discrete points for plotting, increasing the number of points can create the appearance of continuity. The discussion highlights that computers represent real numbers discretely, which affects plotting functions. Ultimately, using `linspace` is the recommended approach for achieving a smooth plot of functions in MATLAB.
sara_87
Messages
748
Reaction score
0

Homework Statement



I have a function y where y = 3x+2
I made x as a vector in matlab:
for i=1:10
x(i) = 4*i

but when i do a plot, i don't want to plot y against the vector x but against x as an argumetn of the function.
In general if i have any function...how do i plot it against its argument (not the argument as a vector)
Thank you

Homework Equations





The Attempt at a Solution

 
Physics news on Phys.org
When you plot y = f(x) each point in your graph is an ordered pair (x, f(x)), so you are plotting a function value against its input argument.

I might not be understanding what you're asking. The only other thing I can think of is that maybe you want to plot y1 = f(x) together with y2 = x. The graph of the latter is a straight line through the origin and whose slope is 1.
 
Sorry, my explanation wasn't very clear.
I want the function to be plot for continuous values on the x axis.
For an example, if i want to plot sin(x), i will do:
x=[0,pi/10,5*pi]
y=sin(x)
plot(x,y)

this will return the function sinx with 0, pi/10, 2pi/10, 3pi/10,...5*pi on the x axis.

But what if i want to plot y=sin(x) from 0 to 5pi with the x-axis being not discrete points but continuous points.

How would i do this?

Thank you
 
I'm still not sure I understand what you're asking. Your x-values are always going to be discrete, but if you use more points, they will be closer together. In your example you are plotting 51 points, with the x-values being pi/10 units apart. If you plot 10 times as many points, the points that are graphed will be closer together.

x=[0,pi/100,5*pi]
y=sin(x)
plot(x,y)
 
I am trying to find a way in Matlab to plot the sin(x) curve without defining discrete points.
I just want the sine curve from 0 to 5pi without defining how many points on the x-axis. is there a way to do this?
 
I don't think there is. To use the plot function you need two arrays: one for the x-axis and one for the y-axis. Necessarily the arrays will have a finite number of elements, so the values will be discrete.
 
that's what i thought. but i thought that there was a way to plot a function (with respect to its argument) with the continuous points on the x axis.
 
Another approach similar to mark44's is to use the linspace function but that's just increasing the size of the x vector and letting Matlab handle the size of the partitions.

x = linspace(0,5*pi,50)
y = sin(x)
plot(x,y)

In the linspace function, you can define how many points you would like to make the vector with the last argument, in this case 50 points. It's been a while since I've plotted but this amount of arguments should give a smooth curve.
 
Thanks.
so i guess i can't plot the function on MATLAB with continuous values on the x axis?
 
  • #10
Computers don't really work with the real numbers of mathematics; they use rational numbers only. In a programming language (including matlab), the representation for a real number is stored in a relatively small amount of storage, typically 4 or 8 bytes and sometimes 10 bytes. This means that real numbers as represented in computers are discrete, with spaces between adjacent numbers. Unlike the real numbers of mathematics, it's not necessarily true for computers that you can always find another real number between any two real numbers.
 
  • #11
I need to plot
x=linspace(0,1)
p=x^3
y=linspace(-1,1)
plot(x,y,p)------?
but I need this data to be in 3 dim with different recangular fig...
 

Similar threads

Replies
10
Views
2K
Replies
6
Views
2K
Replies
6
Views
1K
Replies
5
Views
2K
Replies
1
Views
2K
Replies
1
Views
2K
Replies
1
Views
2K
Replies
1
Views
2K
Back
Top