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

Thread Starter

RRITESH KAKKAR

Joined Jun 29, 2010
2,829
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??
 

Thread Starter

RRITESH KAKKAR

Joined Jun 29, 2010
2,829
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?
 

stahta01

Joined Jun 9, 2011
133
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.
 

Thread Starter

RRITESH KAKKAR

Joined Jun 29, 2010
2,829
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??
 

steveb

Joined Jul 3, 2008
2,436
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??

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.
 

Thread Starter

RRITESH KAKKAR

Joined Jun 29, 2010
2,829
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??
 

steveb

Joined Jul 3, 2008
2,436
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??
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.
 

Jgb5067

Joined Aug 1, 2011
16
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:
Rich (BB code):
>>a = 3;
>>b = 4;
>>a + b
ans = 7
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:
Rich (BB code):
>>c=a+b+ans
c=14
the brackets [] are used for matrices. columns are seperated by spaces and rows by semicolons.
ex
Rich (BB code):
>>a=[1 2 3; 4 5 6]
a= 1 2 3
   4 5 6
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
 

steveb

Joined Jul 3, 2008
2,436
Can we use Mat lab for stimulating circuits,how...
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.
 

steveb

Joined Jul 3, 2008
2,436
OK, can you give some easy example of it???
any circuit.

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.
 

steveb

Joined Jul 3, 2008
2,436
How to write program for this??
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.
 

steveb

Joined Jul 3, 2008
2,436
Can you some other example rather then transfer function??
i don't like tansfer function ( networks subjects)
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.
 
Top