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 fplot help. Something may be wrong with my fplot?

  1. Mar 19, 2014 #1
    1. The problem statement, all variables and given/known data

    The problem was "Plot the following equation with MatLab, for values of x from -3 to 6.

    2. Relevant equations

    y = x^4 - 4x^3 - 6x^2 + 15

    3. The attempt at a solution

    I created a function.

    function r = graph(x)
    for i = 1:x
    r(i) = i^4 - 4*i^3 - 6*i^2 + 15;

    Then I passed fplot(graph(10), [0 10000]) through the command prompt.

    It returned

    Error using fcnchk (line 107)
    FUN must be a function, a valid string expression,
    or an inline function object.

    Error in fplot (line 61)
    fun = fcnchk(fun);

    Any ideas on what this means?
  2. jcsd
  3. Mar 20, 2014 #2


    User Avatar

    Staff: Mentor

    Why does your function return a vector?

    Why are you plotting from 0 to 10000? (And there is a comma missing.)

    (Note: please use the CODE tags to delimit code.)
  4. Mar 20, 2014 #3
    I needed a vector to plot it, right?

    they were just the first two numbers I thought of.
  5. Mar 20, 2014 #4


    User Avatar

    Staff: Mentor

    Not if you are using "fplot". You need to define a function, and "fplot" will call it as needed to generate to plot.

    Alternatively, you can define a vector of x values, then build a vector of the corresponding y values, and use "plot" to plot it.

    Think a bit about what you did in the function. Given a value of x, you generate a vector that contains the values of ##y(x)## for ##x## the integers between 1 and x.

    I'll also give you some advice about coding polynomials. You should never write
    Code (Text):
    r(x) = a4*x^4 +a3*x^3 + a2*x^2 + a1*x + a0;
    as this can lead to a big rounding error (depending on the value of x and the coefficients of the polynomial). You should use instead
    Code (Text):
    r(x) = (((a4 * x + a3) * x + a2) * x  + a1) * x + a0;
    But the problem tells you what the range of the plot should be.
  6. Mar 20, 2014 #5
    I thought that meant to use a for loop to use each of those values. That's why I used for i = 1:10
  7. Mar 20, 2014 #6


    User Avatar

    Staff: Mentor

    That would have given you the values for ##x \in [1,10]##, not ##x \in [-3,6]## as the problem asks.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted