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: What is wrong with this matlab code?

  1. Feb 9, 2012 #1
    Write a user-defined MATLAB function for the following math function:
    y(t) = -0.2x^4 + e^(-0.5x)*x^3+7x^2
    The input to the function is x and the output is y. Write the function such that x can be a vector

    Use this function to make a plot of the function y(x) -3≤x≤4

    function y=chp7one(x)
    y=-0.2*x.^4+exp(-0.5*x)*x.^3 + 7*x.^2;
    x=[-3 -2 -1 0 1 2 3 4];

    Error in ==> chp7one at 2
    y=-0.2*x.^4+exp(-0.5*x)*x.^3 + 7*x.^2;
    Last edited: Feb 9, 2012
  2. jcsd
  3. Feb 10, 2012 #2
    Any idea ? (Urgent)
  4. Feb 10, 2012 #3


    Staff: Mentor

    I'd try it one term at a time to see which one doesnt like. I'm suspicious of the second term between the exp(...)* x shouldn't that be exp(...).* x

    Just a thought I use freemat not matlab.
  5. Feb 10, 2012 #4
    Yes, but i still get error messages
  6. Feb 10, 2012 #5


    Staff: Mentor

    try this, it worked on my version:

    y=-0.2.*x.^4+exp(-0.5.*x).*x.^3 + 7.*x.^2

    just added a few more dots to the mix
  7. Feb 10, 2012 #6


    Staff: Mentor

    I have a couple of questions:
    How can you calculate the y values before you set the x array?
    Why are you setting x in your chp7one function when it is being passed in as a parameter?
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook