# Plotting a Function Against Its Argument in MATLAB

• sara_87
In summary, you can't plot a function with continuous values on the x axis if you want the curve to be smooth. You need to use a linspace function and specify the number of points you want.
sara_87

## 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

## The Attempt at a Solution

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?

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.

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...

## 1. What is the syntax for plotting a function in MATLAB?

The syntax for plotting a function in MATLAB is plot(argument, function). The argument is the independent variable and the function is the dependent variable.

## 2. How do I change the color or style of my plot in MATLAB?

To change the color or style of a plot in MATLAB, you can use the optional Color and LineStyle arguments in the plot function. For example, plot(x, y, 'r--') will plot a red dashed line.

## 3. 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 will prevent the previous plot from being erased and allow you to add additional plots to the same graph.

## 4. How do I add a title and labels to my plot in MATLAB?

To add a title and labels to your plot in MATLAB, you can use the title, xlabel, and ylabel functions. For example, title('My Plot') will add a title to your plot.

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

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

• Engineering and Comp Sci Homework Help
Replies
10
Views
2K
• Engineering and Comp Sci Homework Help
Replies
6
Views
1K
• Engineering and Comp Sci Homework Help
Replies
1
Views
1K
• Engineering and Comp Sci Homework Help
Replies
3
Views
1K
• Engineering and Comp Sci Homework Help
Replies
1
Views
903
• Engineering and Comp Sci Homework Help
Replies
1
Views
1K
• Engineering and Comp Sci Homework Help
Replies
7
Views
1K
• Engineering and Comp Sci Homework Help
Replies
2
Views
1K
• Calculus and Beyond Homework Help
Replies
10
Views
1K
• Engineering and Comp Sci Homework Help
Replies
3
Views
2K