• Support PF! Buy your school textbooks, materials and every day products Here!

Matlab plot

  • Thread starter sara_87
  • Start date
  • #1
763
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 dont 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

 

Answers and Replies

  • #2
33,508
5,193
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.
 
  • #3
763
0
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
 
  • #4
33,508
5,193
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)
 
  • #5
763
0
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?
 
  • #6
33,508
5,193
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.
 
  • #7
763
0
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.
 
  • #8
7
0
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.
 
  • #9
763
0
Thanks.
so i guess i cant plot the function on matlab with continuous values on the x axis?
 
  • #10
33,508
5,193
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...
 

Related Threads on Matlab plot

  • Last Post
Replies
5
Views
2K
  • Last Post
Replies
1
Views
1K
  • Last Post
Replies
1
Views
811
  • Last Post
Replies
6
Views
592
  • Last Post
Replies
1
Views
8K
  • Last Post
Replies
2
Views
1K
  • Last Post
Replies
1
Views
2K
  • Last Post
Replies
1
Views
505
  • Last Post
Replies
5
Views
23K
  • Last Post
Replies
4
Views
529
Top