Lec2

Post on 21-Dec-2014

208 views 5 download

Tags:

description

 

Transcript of Lec2

Matlab 7

Lec 2-Fundamentals

Topics

variables, operators and expressions;

arrays (including vectors and matrices);

basic input and output;

repetition (for);

decision (if);

Variables

must start with a lettercan be alphabets (a-z), digits 0-9,underscore(_)may be as long as you likeMatlab rem only first 31 charactersEg r2d2 dhoni walle nano etcPay – day ,2s , name$ galtiyaaaaaaaannnn????????

Official Matlab Documentation refers variables as Arraysscalar or vector

error message:

??? Undefined function or variable ...

Matlab is Case Sensitive

camel Caps method eg. camelCaps

Command and Function names are case sensitive

Workspace

variables you create during a session remain in workspace

untill you clear them using clear

whos – size of variables who diff dekho

ans – function=lastvalue of exp evaluated but not

assigned to a variable

example socho!

double = holds a numeric value = double precision

floating point

clear vname1 vname2

Workspace Browser

the myconstant.m example

g=9.8;

avo=6.23e23

run myconstant at start of session,Workspace is loaded

Arrays : Vectors and Matrices

Basic Idea Of a Matrix – Rows and Columns

Vector – Special Type of Matrix,1 Row,1 Column;

called list or arrays in some other Prog Lang

Matlab Handles Vectors and Matrices in a Same Manner

Vectors

Enter a statement in command line :

x = [1 3 0 -1 5]

Now enter disp(x) to see vector kaise dikhta hai

Also try y = [2,3,5]

Then whos command or size(y) command

Iska o/p kya hoga ? : - x =[130-15] bataaaaaoooooo?????

a = [ 1 2 3]

b = [4 5]

c = [ a –b]

Iska o/p bataaaaaooo ab????????

Iske bare me kya khyal hai?? :

a = [1 3 7];

a = [a 0 -1];

Vector can also be initialized as

x = 1:10;

x = 1:0.5:4;

x=1:2:6 try karo! Kya galti hai statement me????

x=0:-2:-5

This is list method of initializing!make sure

1.Elements in list must be enclosed in square bracket

2.Shud be separated by either space or commas

Ek aur tarika hai ki ap linspace function use karo!

Yeh try karo

linspace(0,pi/2,10)

Ye 10 equally space points ka vector create karta hai

0 and pi include karke

Transposing vectors sanjhte hai ab:::

Abhi tak Row vectors me khel rahe the abhi column vectors me

ate hai!

U need to transpose the row vectors to get column vectors

Try this

y=[ 1 4 8 0 -1]’

Sign used is the aposthrophe

Ab ati hai vectors ki subscripts:

Try this

Row vector of 7 random nos

r = rand(1,7)

Ab try karo

r(3)

Isme joh no. 3 dale hai who subscript hai!

Iske jawab do sochke and karke

r(2:4)

r(1:2:7)

r([1 7 2 6])

Conclusion abt subscripts:

Subscript is indicated inside () brackets

It may be a scalar or vector

Subscript always starts at 1

Fractional subscripts r always rounded down

x(1.9) will be x(1)

Matrices

Matrix = rows and columns

Try this

a = [ 1 2 3;4 5 6 ]

Ab apna transpose ka knowledge jhalkao!!!!

Try a’

Hum matrix same length ke vectors se bhi bana sakte hai!

x=0:30:180;

table = [ x' sin(x*pi/180)']

Capturing o/p

Either copy paste

Ya try this

diary kp2

diary off

DANCE TIME SURPRISE

Problem statement:

The equation for vertical motion under gravity is given by

S = ut – ½ gt^2

We ve to compute the value of s over a period of abt 12.3 sec

At intervals of 0.1 sec

Initial velocity 60 m/sec lijiye g = 9.8

M-file banaiye

Sabse pahile 4 points ka structure plan sochiye and paper pe likhiye!next silde se match karke dekhiye ki kya ap sahi hai ya ap kitte sahi hai????

Structure Plan

1. Assign the data (g, u and t) to MATLAB variables.

2. Calculate the value of s according to the formula.

3. Plot the graph of s against t.

4. Stop.

t wala statement vector initialize karta hai

. Operator ko dekho ye vectors ke liye hai!bad hum discuss karenge!

Formula for s is evaluated for every element of vector t making another vector

Disp command ka use table display ke liye

,,,, and … ka use

OVERRRRRRRRR aj ke liye