You are on page 1of 73

-

2010

,

,


.




.

.
.

.

.
2010- 11- 19


:
1.1
1.2
1.3
:
:
3.1
3.2
3.3
3.4
3.5
M-file :
4.1 m-files
4.2 m-files
4.3
: Loops
if statement 5.1
for statement 5.2
while statement 5.3
:
6.1
6.2
6.3

1.1
Command Windows _1
.
Commands , statements ,
functions
, Commands
current directory _2
, Editor

Command History_3

1.2 commands

command windows


(
1.2

) (1.2
(

X


:
x=2
y=9
y+x
ans=11


Small dont equal capital

32
for ,while , find .etc

1.1


ans
pi

complex
3+4j=3+4i

inf
1/0
NaN Indicates an undefined numerical
result

NaN
.
0/0
1.1
i,j


:
x=2 , y=x+2, z=x+y

X =2
Y= 4
Z=6

1.3


Matlab form
A^B
A*B
A/B
A\B=B/A
A+B
A-B

Operation

Symbol
^
*
/
\
+
Example 1 :

Write using matlab the following


10 18
+
+ 5(9) 2
)A - 13 5(7
6

6(351/ 4 ) + 140.35

B-

Solution:
A
6*(10/13)+18/(5*7)+5*9^2
Ans =410.1297
B
6*35^(1/4)+14^.35
Ans =17.1123

***
,

Problems
1.1 Find the value of z if you known that x=2,y=17

3
x2+y2

Z =x3 + y 2 +
Ans:


x=2
y=3
))z= x^3+y^2+(3*pi/(x^2+y^2
z=17.2

1.2 Find the power P if you known current I=10A and resister R=10
P=IV , V=IR
Ans:


; I = 10
; R= 10
; V=I*R
P=I*V
P = 1000

1.3
h r

v = r 2h
15 8
% 20

Ans:

;r=8
; h = 15
;v=pi*r^2*h
;v=v+0.2*v
r=(v/pi/h)^.5
r=78.8
:

-1
,

-2 e
e
3*104 3e 4

log , ln , exponential -3

-4
sin(x)

sin x

asin(x)

sin-1x

cos tan cosh tanh


: radian
-5 sqrt
sqrt(x)= x

comlex numbers -6

complex

X+Yi or X+Yj
complex



)abs(x

angle(x)


complex real(var)
imag(var)
Example 1 :
Given x =-5 + 9i and y =6-2i, use MATLAB to show that
x + y = 1+ 7 i, xy = -12 + 64i , and x / y = -1.2 + 1.1i.

clear and clc -7


clear
X
clear
clear

clc
clc CW

-8 exist
0 1
)'exist('var
-9 who , whos


who
whos complex
-10 ...

-11 quit

-12 format

format short
format long
format +

ceil , floor and round-13



ceil
floor
round

-14

save filename var1
var2 . etc
load filename
12
: 8.85*10 .

tab

mechatronics = 1990/11/28
tab
,
enter

:
calendar
clock

beep

:
Arrays

3.1

8 3
2
6 4
2
6 9 4 j


,
,

:

8 3
2 1
6 4
2 *2
6 9 4 j 3

..

3.2
,


][ space
enter ;

..

3.3

-
.

: 230,543,623,75,90
170

: 170

) sum(var

) sum(ans ) sum(x
: x ans

: sum

:

-

: =
:

3.4
length -

size -


)size(x,1

)size(x,2

: 2 10 2
.. 260 .

roots -

:

x 3 13x 2 18x 12



inv -
,

( = )

: x,y,z
4x+12y+23z=12
34x+4y-3z=-90
x-32y-9z=0

4 12 23 x 12
34 4 3* y 90
1 32 9 z
0

x,y,z
,


linspace

)linspace(initial, final, steps

) : max(a
.

3 4 5
23 3 4
12 34 5
:

) : min(a
) : prod(a ,

)ones(n,m
)eye(n
)zeros(n,m
) : diag(A
)magic(n
)]randint(n,m,[i,f
n m I f ..

3.5

problems
3.1 Use MATLAB to determine how many elements in the array
[cos(0) : 0 . 02 : log10(100)] . Use MATLAB to determine the
25th element. (Answer: 51 elements and 1.48.)
3.2 Use MATLAB to find the roots of the polynomial 290 - 11 x + 6x2
+x3 .
(Answer: x = - 10,2 5i.)
3.3 The voltage across a discharging capacitor is
v(t) 10(1e0.2t )
Generate a table of voltage, v(t) , versus time, t, for t = 0 to 50
seconds with increment of 5 s.
3.4


M-files

4.1 M-file
,


M-file C++
. M-file command windows
CW

.
M-file
File >> New >> M-file

, cw
.m

: M-file clear clc


clc

.

4.2 M-files
disp -1
cout C++

input -2
cin c++

' 's

string number


string num2str or str2num
if for .

: :

0r A
d

. c A
10 8.85e-12

: . d

Menu -3

)k=menu('title','option1',option2',.etc
.
plot -4
plot
)plot(var1,var2
: 180-0

4.3

<
>
=<
=>

==
=~
&
!
~



And
Or
Not

: ) var(var1 compare var var


var1 var

find

: \n

\n x .. .
M-file .
Loops .
.

problems
4.1
:
;)' z=input(' the lift of equal matrix:
;)' w=input(' the right of equal matrix:
;)q= inv(z
s= q*w

4.2 LR

0 20

4.3
induction machines dc , no-load ,
locked rotor

4.4 ,
,


c++

if statement 5.1
if

simple if statement
nested if statement
if-else statement

simple if statement- 1


if expression 1
statement group 1
end

nested if statement- 2

if expression 1
statement group 1
if expression 2
statement group 2
end
statement group 3
end
statement group 4

if-else statement-3

if expression 1
statement group 1
else
statement group 2
end
end

.
if
if (
)
else


: 1

: .

num2str
string numbers W
strings x & z .
x
)' x=input('enter number','s x
.

: 2
good
luck .
: .
if

floor .

: 3
please enter positive number
:

: 4

:
.
else else

for statement 5.2



for i:s:f
statement group X
end
for
: i
:s
1
: f
for
end

: ( factorial)
: )factorial(var or num
:

Break & continue


break
Loop continue " if
"

break

break for

15 4x 10
x 9
y { 10x 10
0 x 9
x 0

10

-5<=x<=30

:
;for x=-5:30
if x>=9
; y=15*sqrt(4*x)+10
elseif x>=0
;y=10*x+10
else
;y=10
end
;q(x+6)=y
;r(x+6)=x
end
q
r
)plot(r,q

.
: 15

5k 2 2k

while statement 5.3



while expression 1
statement group 1
end
statement group 2
expression 1 statement group
1 . 2

Problems
5.1

100_ 90
90_ 80
80_ 60
60_ 40
40_ 0

A
B
C
D
F

enter
F A
" average"
:
clear
a=0;b=0;c=0;d=0;f=0;
e1=0;
e2=0;e3=0;e4=0;e=0;
x=1;
N=input('enter number of student')
while x<=N
z(x)=input('enter the mark')
if z(x)> 100
x=x-1;
z(x+1)=[];
disp('marks between 0-100 >>retry')
elseif z(x)>=90
e=e+1;
a(e)=z(x);
elseif z>=80
e1=e1+1;
b(e1)=z(x);
elseif z>=60
e2=e2+1;
c(e2)=z(x);
elseif z>=40
e3=e3+1;
d(e3)=z(x);
elseif z>=0
e4=e4+1;
f(e4)=z(x);
else
z(x)=[];
x=x-1;
disp('enter positive marks only')
end
x=x+1;
end
Avarage_of_marks=sum(z)/N
Average_A=[num2str(e/N*100),'%']

]'Average_F=[num2str(e4/N*100),'%

5.2

w= 2f L=4

C = 5F,

f 0 60 Hz
:
Z
f Z

,

.
.
.

Symbolic and Numerical 6.1


.



f(x)=x^2-x
Error



syms var1 var2 var3

syms
x,y,z,
syms x y z
f x^2-2 2
f 2 f
) f(x

: inline

6.2 .
diff
()
:
)'diff('var


x
eval

:
-

) f (x
x1 x 0
) f (x
x1 & x0
:
syms x
)'d=input('enter equation x \n
;)f=inline(d
;))z=diff(f(x
;)f1=inline(z
)')x0=input('enter approximate value of f(x
x=x0
for u=0:inf
y=x
;))x=y-(f(x)/f1(x
if x==y
break
end
end
x

6.3
. int diff
1/x 5-2

You might also like