1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Matlab plot

  1. Dec 11, 2009 #1
    1. The problem statement, all variables and given/known data

    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

    2. Relevant equations

    3. The attempt at a solution
  2. jcsd
  3. Dec 11, 2009 #2


    Staff: Mentor

    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.
  4. Dec 11, 2009 #3
    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:

    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
  5. Dec 11, 2009 #4


    Staff: Mentor

    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.

  6. Dec 11, 2009 #5
    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?
  7. Dec 11, 2009 #6


    Staff: Mentor

    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.
  8. Dec 11, 2009 #7
    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.
  9. Dec 11, 2009 #8
    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)

    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.
  10. Dec 11, 2009 #9
    so i guess i cant plot the function on matlab with continuous values on the x axis?
  11. Dec 11, 2009 #10


    Staff: Mentor

    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.
  12. Oct 24, 2011 #11
    I need to plot
    but I need this data to be in 3 dim with different recangular fig...
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook