Matlab staring with basic.........

Discussion in 'Programmer's Corner' started by RRITESH KAKKAR, Jun 15, 2011.

  1. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    Hi,

    I want to know few question, like why we use this ([.......]) ??
    and if we write code after placing this;(semicolon) why?? the line does not increases??
     
  2. steveb

    Senior Member

    Jul 3, 2008
    2,433
    469
    Can you expand on what the questions mean? It's not too clear to me what you are asking. Maybe some examples will let the context make it clearer.
     
    RRITESH KAKKAR likes this.
  3. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    OK,
    I was watching tutorial video of it, there was written sqrt([1 2 3])...,etc
    this is one of the e.g...

    &
    >> a=[1 2 3]

    a =

    1 2 3

    >> a=[1 2 3];
    >>

    see the diff. in them.....why it happen?
     
  4. stahta01

    Member

    Jun 9, 2011
    133
    21
    In Matlab the ";" is used to NOT show the result of the line; by default it shows the result.

    After you get the code to work it is common to add ";" to all or most of the lines to hide unneeded output.

    Tim S.
     
    RRITESH KAKKAR likes this.
  5. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    Hi again,

    In mat lab while creating a m files, we play it to have mfiles on command window or we can simply write it in c.w. like type filename.m

    Is there any diff. in both of them??
     
  6. steveb

    Senior Member

    Jul 3, 2008
    2,433
    469

    If you are asking whether there is a difference in running m-files directly from the command window versus running it by hitting the play button in the m-file editor, then i think that there is no difference at all. It's just a matter of convenience.
     
    RRITESH KAKKAR likes this.
  7. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    Hi again,

    I saw edit command in lecture video, what the use of it as by doing this m files open??
    & clc clear comm. window but what about clear command what does it do??
     
  8. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    Reply pls........
     
  9. steveb

    Senior Member

    Jul 3, 2008
    2,433
    469
    Note that Matlab has a "help" command to give you information about any command. The "edit" command simply opens an m.file (i.e. script file) in the text editor.

    Both "help" and "edit" were more useful in the old days when matlab was run in a command window only. Now, you have a windows based system to open files and get help.

    The "clear" command clears the variables. Use the "help clear" command to get help information about all the options available under this command.
     
    RRITESH KAKKAR likes this.
  10. Ramesh Prabhu

    New Member

    Jul 6, 2011
    1
    1
    Hi, am a new one to learn Matlab programming,Can anyone send me some basic course materials to learn programming pls...My mail id..ramesprabu@gmail.com
     
    Last edited: Jul 7, 2011
    RRITESH KAKKAR likes this.
  11. Jgb5067

    New Member

    Aug 1, 2011
    16
    2
    basically whenever you run a script or a simple equation in the matlab command window, Matlab saves the answer. the clear commmand just clears all those variables
    ex:
    Code ( (Unknown Language)):
    1.  
    2. >>a = 3;
    3. >>b = 4;
    4. >>a + b
    5. ans = 7
    6.  
    if you clear this a and b will not have a value and you will not be able to add them
    until setting new values to them. but not clearing it will allow you to use a, b, and ans for other equations, but ans will change to whatever the answer is if you dont set another variable for the answer
    ex:
    Code ( (Unknown Language)):
    1.  
    2. >>c=a+b+ans
    3. c=14
    4.  
    the brackets [] are used for matrices. columns are seperated by spaces and rows by semicolons.
    ex
    Code ( (Unknown Language)):
    1.  
    2. >>a=[1 2 3; 4 5 6]
    3. a= 1 2 3
    4.    4 5 6
    5.  
    sorry if the formatting is a little off in the code tags, i dont have matlab here to really work them out. but i hope this helps a little, and like the previous poster, there is always the help funtion
     
    RRITESH KAKKAR likes this.
  12. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    Can we use Mat lab for stimulating circuits,how...
     
  13. steveb

    Senior Member

    Jul 3, 2008
    2,433
    469
    Yes. You do it by deriving the circuit equations, rather than hooking up components. Depending on the type of system, various Matlab tools and functions can be used. Linear systems are particularly well-suited for analysis with Matlab. Nonlinear systems can be simulated using the numerical time-domain solvers available in Matlab, but it's usually easier to do that in Simulink.
     
    RRITESH KAKKAR likes this.
  14. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    OK, can you give some easy example of it???
    any circuit.
     
  15. steveb

    Senior Member

    Jul 3, 2008
    2,433
    469

    A simple example is a low pass filter, RC type. This is a voltage input, into a resistor in series with a capacitor, with the output voltage taken across the capacitor.

    If you write the equations for the transfer function you get

    T=(1/(RC))/(s+1/(RC))

    the system can be defined with the "tf" command in matlab

    sys=tf([1/R/C],[1 1/R/C])

    with real values stored for R and C.

    From there, you can look at step responses, impulse responses, Bode plots etc. using the Matlab commands.

    impulse(sys)
    step(sys)
    bode(sys)

    This just scratches the surface, but you asked for an easy example.

    You can get more advanced and use state space representations with the "ss" command. You can also define multiple systems and then hook them together to make a larger system. This is done with the "connect" command.

    This still only scratches the surface. There are also various tools for analysing systems and optimizing control feedback systems.

    A decent book to learn from is "Signals and Systems with Matlab computing and Simulink Modeling" 3rd edition, by Steven T. Karris. This book goes beyond circuit analysis, but does include some examples of that.

    If you want to model nonlinear circuits involving transistors and OPAMPs, diodes etc., you can either linearize them using some mathematical tricks and then use matlab, or you can develop a nonlinear state space representation of the system and use the numerical equations solvers. Matlab has all the tools for this; however, I'd recommend using Simulink if you need to work at that level.
     
    RRITESH KAKKAR likes this.
  16. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    How to write program for this??
     
  17. steveb

    Senior Member

    Jul 3, 2008
    2,433
    469
    Programs for this type of thing can be very simple and straightforward. Start by defining component values as variables and set them to a number.

    e.g.

    R=100 % resistor value in Ohms
    C=1e-6 % capacitor value in Farads

    Then define the system transfer function using the "tf" command (transfer function model), "ss" command (state space model) or "zpk" command (zero pole gain model).

    e.g.

    sys=tf([1/R/C],[1 1/R/C]) % define transfer function for low pass filter

    Then run any plots you would like, depending on what you are interested in calculating either in the time domain or in the frequency domain.

    e.g.

    impulse(sys) % plot the impulse response of the sys system
    step(sys) % plot the step response of the sys system
    bode(sys) % plot the frequency response (as Bode plot) for the sys system

    Note that any of the above commands can be understood by reading the online help using the "help foo" command, where "foo" is the name of any command.
     
    RRITESH KAKKAR likes this.
  18. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    Can you some other example rather then transfer function??
    i don't like tansfer function ( networks subjects)
     
  19. steveb

    Senior Member

    Jul 3, 2008
    2,433
    469
    Well, what do you like? You can use state space models with the "ss" command. This requires specification of the A, B, C and D matrices.

    The alternative to LTI models with tf, ss and zpk is direct solving of differential equations in the time domain. There are Matlab functions for this, but I always do this type of modeling using Simulink.

    I'd rather not start guessing about what you want to do. Please specify. It's quite possible that Matlab does not do things in the way you like. For example, you can't just hook up circuit components and push run, like in SPICE simulators.
     
    RRITESH KAKKAR likes this.
  20. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    then what the use of learning matlab if we can do easily in stimulator software??
    like C to assembly.!!
     
Loading...