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: Anyone know MATLAB?

  1. Sep 24, 2009 #1
    anyone know MATLAB???

    1. The problem statement, all variables and given/known data
    Using Matlab, write an m-file that creates subplots of y=x^n for n=[1:6] and
    x = [-10:0.01:10]. Each subplot needs appropriate labels in bold and using superscripts. this should also be commanded from within a loop and using num2str commands where necessary.

    3. The attempt at a solution

    i started out with a nested for loop as followed.

    for x = [-10:0.01:10]
    for n=[1:6]
    plot(y) trying to just graph what im getting so far in the loop

    when i run the program i get just a blank graph??
    any help would be great!!!
  2. jcsd
  3. Sep 25, 2009 #2


    User Avatar
    Science Advisor

    Re: anyone know MATLAB???

    Well, the beauty of MATLAB is that you can run operations on vectors and arrays, and don't have to do element-wise operations (most of the time), as you might in C or C++ (or whatever your intro computing course was).

    So, you could do something like the following:
    Code (Text):

    y=x.^1     %note the period before the ^, this tells MATLAB to raise each element of x to the power of 1

    pause(1)     %pauses a second

    Now, you could pack each of your y-vectors into a 2-D matrix (array), and sort things out later, or you can use a cell array (the curly braces), and store the vectors separately.

    Code (Text):

    for n=1:6
    plot(x,y{1}, x,y{2}, x,y{3}, x,y{4}, x,y{5}, x,y{6})
    BTW, a great way to find what MATLAB functions do is just to type 'help' and the name of the function, for instance:
    >> help plot

    ...which brings up the help file for the Plot function. More in-depth documentation is available at the MATLAB website (I found this invaluable when I was a MATLAB programmer one summer--bookmark the top page!):
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook