Thread: Matlab help

  1. #1
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    I just started learning Matlab again after not having used it in months, and got some odd results -

    Trying to plot tan(x), from -pi/2 to pi/2 -
    Code:
    X = (-pi/2):pi/100:(pi/2);
    for i = 1:length(X)
         Y(i) = tan(X(i));
    end;
    plot(Y);
    I ended up getting something that has a negative spike at 0 and positive spike at 100. Certainly doesn't look like tan(x) to me.
    I'm sure it's something simple, but it's been an hour already...

    Thanks!
    [edit]
    Ah! Ok. It seems like the big dynamic range threw Matlab off somehow. Adding clamping works.
    [/edit]
    Last edited by cyberfish; 03-07-2011 at 03:55 AM.

  2. #2
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    You don't need that for loop, tan() and many other functions accept/output arrays:

    Y = tan(X);

    Also, you need to plot against an independent variable:

    plot(X,Y)

    It looks sharp because pi/100 isn't a small enough step size, try .01 lower
    Last edited by Epy; 03-07-2011 at 06:56 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Matlab Help
    By anirban in forum Tech Board
    Replies: 1
    Last Post: 09-02-2010, 05:00 AM
  2. Convert Matlab Algebraic Sol to C compatible
    By Takeshi in forum C Programming
    Replies: 3
    Last Post: 01-08-2009, 06:35 PM
  3. MATLAB importing C libraries
    By chico1st in forum C Programming
    Replies: 1
    Last Post: 05-29-2008, 02:01 PM
  4. MATLAB user trying to use gcc in cygwin (with BLAS and LAPACK)
    By greek_tragedy in forum C Programming
    Replies: 0
    Last Post: 07-23-2007, 02:29 PM
  5. C/C++ and Matlab?
    By darkwalk in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2006, 03:00 AM