Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Beginner in matlaB i need lil help

  1. Oct 2, 2009 #1
    hello everyBody..
    am a beginner in matlab i need lil help in the equation blew.. and if anyone has any tutorial for matlab i'll b more than welcome to accept them :P

    y=e^x . cos x

    plot : pi greater than x greater than 0
    then plot : -pi smaller than x smaller than one

    then combine the two graphs

    i tried to do it but i get alot of errors,,

    my trial :
    y=exp(x)* cos(x)
    subplot (3,1,1)
    plot (x,y)

    y1=exp(x)* cos(x)
    subplot (3,1,2)
    plot (x1,y1)

    plot (y,y1)


    thanks in advance..
  2. jcsd
  3. Oct 2, 2009 #2
    Your problem is that: in the first line, you declare x as an array. In the second line, you force matlab to calculate the exp and cos value for each member of x array. Therefore, matlab throws error because exp and cos funtion in matlab dont know how to do that but they only accept 1 value for argument per calculations.. To avoid this problem, you should use loop. For example:

    Code (Text):

    for j = 1 : length(x)              // for loop
        y=exp(x(j))* cos(x(j));     // pick member x(j) to calculate

    plot (x,y)
    //x(A) will give you value of Ath member of array x (A is integer and smaller than size of x).

    Loop is crucial concept for programming that you must obtain, such as FOR loop, WHILE loop, DO loop.

    For more information about operators and functions in matlab, check FUNCTION REFERENCE in matlab help section.

    Hope this help you.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook