lec2
DESCRIPTION
TRANSCRIPT
![Page 1: Lec2](https://reader033.vdocuments.net/reader033/viewer/2022051818/54964a56b47959e1148b45cc/html5/thumbnails/1.jpg)
Matlab 7
Lec 2-Fundamentals
![Page 2: Lec2](https://reader033.vdocuments.net/reader033/viewer/2022051818/54964a56b47959e1148b45cc/html5/thumbnails/2.jpg)
Topics
variables, operators and expressions;
arrays (including vectors and matrices);
basic input and output;
repetition (for);
decision (if);
![Page 3: Lec2](https://reader033.vdocuments.net/reader033/viewer/2022051818/54964a56b47959e1148b45cc/html5/thumbnails/3.jpg)
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 ...
![Page 4: Lec2](https://reader033.vdocuments.net/reader033/viewer/2022051818/54964a56b47959e1148b45cc/html5/thumbnails/4.jpg)
Matlab is Case Sensitive
camel Caps method eg. camelCaps
Command and Function names are case sensitive
![Page 5: Lec2](https://reader033.vdocuments.net/reader033/viewer/2022051818/54964a56b47959e1148b45cc/html5/thumbnails/5.jpg)
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
![Page 6: Lec2](https://reader033.vdocuments.net/reader033/viewer/2022051818/54964a56b47959e1148b45cc/html5/thumbnails/6.jpg)
the myconstant.m example
g=9.8;
avo=6.23e23
run myconstant at start of session,Workspace is loaded
![Page 7: Lec2](https://reader033.vdocuments.net/reader033/viewer/2022051818/54964a56b47959e1148b45cc/html5/thumbnails/7.jpg)
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
![Page 8: Lec2](https://reader033.vdocuments.net/reader033/viewer/2022051818/54964a56b47959e1148b45cc/html5/thumbnails/8.jpg)
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????????
![Page 9: Lec2](https://reader033.vdocuments.net/reader033/viewer/2022051818/54964a56b47959e1148b45cc/html5/thumbnails/9.jpg)
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
![Page 10: Lec2](https://reader033.vdocuments.net/reader033/viewer/2022051818/54964a56b47959e1148b45cc/html5/thumbnails/10.jpg)
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
![Page 11: Lec2](https://reader033.vdocuments.net/reader033/viewer/2022051818/54964a56b47959e1148b45cc/html5/thumbnails/11.jpg)
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
![Page 12: Lec2](https://reader033.vdocuments.net/reader033/viewer/2022051818/54964a56b47959e1148b45cc/html5/thumbnails/12.jpg)
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])
![Page 13: Lec2](https://reader033.vdocuments.net/reader033/viewer/2022051818/54964a56b47959e1148b45cc/html5/thumbnails/13.jpg)
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)
![Page 14: Lec2](https://reader033.vdocuments.net/reader033/viewer/2022051818/54964a56b47959e1148b45cc/html5/thumbnails/14.jpg)
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)']
![Page 15: Lec2](https://reader033.vdocuments.net/reader033/viewer/2022051818/54964a56b47959e1148b45cc/html5/thumbnails/15.jpg)
Capturing o/p
Either copy paste
Ya try this
diary kp2
diary off
![Page 16: Lec2](https://reader033.vdocuments.net/reader033/viewer/2022051818/54964a56b47959e1148b45cc/html5/thumbnails/16.jpg)
![Page 17: Lec2](https://reader033.vdocuments.net/reader033/viewer/2022051818/54964a56b47959e1148b45cc/html5/thumbnails/17.jpg)
DANCE TIME SURPRISE
![Page 18: Lec2](https://reader033.vdocuments.net/reader033/viewer/2022051818/54964a56b47959e1148b45cc/html5/thumbnails/18.jpg)
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????
![Page 19: Lec2](https://reader033.vdocuments.net/reader033/viewer/2022051818/54964a56b47959e1148b45cc/html5/thumbnails/19.jpg)
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.
![Page 20: Lec2](https://reader033.vdocuments.net/reader033/viewer/2022051818/54964a56b47959e1148b45cc/html5/thumbnails/20.jpg)
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
![Page 21: Lec2](https://reader033.vdocuments.net/reader033/viewer/2022051818/54964a56b47959e1148b45cc/html5/thumbnails/21.jpg)
OVERRRRRRRRR aj ke liye