Introduction in
MATLAB FOR
SCIENCE & ENGINEERING APPLICATIONS
FALL – 2001
1
Ch.1:Matlab and Problem Solving 1)Entering Commands: we can use matlab as a calculator to apply the mathmatics operations. » r=8/10 r = 0.8000
type r » r r = 0.8000
2)Obtaining Plot: plot: y=sin2x for 0<=x<=10 x=[0:.1:10]; » y=sin(2*x); » plot(x,y),xlabel('x'),ylabel('sin(2x)')
1 0.8 0.6 0.4 sin (2x )
0.2 0 -0.2 -0.4 -0.6 -0.8 -1
0
1
2
3
4
5 x
6
7
8
9
10
3)Problem Solving Methodology: a- understand the purpose of the problem. b- collect the knoun information, realize that some of it might be discovered later. c- determine what informatiom you must find. d- simplify the problem only enough to allow the required information to be obtained. e- draw a sketch and label any necessary variable. f- determine which fundemental principles are applicsable. g- think about your proposed solution approach in general . h- label each step in the solution process. i- hand check results using a simple version of the problem, check dimentions and units.
2
Example of problem solving:
Consider the following simple example of the steps involved in problem solving, Suppose you work for a company that produces packaging. You are told that a new packaging material can protect a package when dropped, provided that the package hits the ground at less than 25 feet per second. The package’s total weight is 20 pounds. You must determine whether the packaging material provides enough protection when the package is carried by delivery persons. a- understand the purpose of the problem. The implication here is that the packaging is intended to protect against being dropped while the delivery person is carry it. b- collect the knoun information, realize that some of it might be discovered later. The known information is the package’s weight, and maximum allowable impact speed c- determine what informatiom you must find. You need to determine the maximum height from which the package can be dropped without damage. d- simplify the problem only enough to allow the required information to be obtained. The following assumptions will simplify the problem: * The package is dropped from rest, with no vertical or horizontal velocity. * The effect of air drag is negligible. * The acceleration g due to gravity is constant. e- draw a sketch and label any necessary variables. The sketch give you impression about the case. f- determine which fundemental principles are applicsable. Because this problem involves a mass in motion. Newton’s laws are applicaple: * h=0.5gt². * v=gt. * mgh=0.5mv². g- think about your proposed solution approach in general . you have select the best equation that gives the solution. h- label each step in the solution process. * basic principle: h=0.5v²/g. * determine the value of constant g: g=32.2ft/sec². * use the given information to perform the calculation: h=0.5*25²/32.2=9.7 feet. By Matlab: » g=32.2; » v=25; » h=v^2/(2*g) h = 9.7050
i- hand check results using a simple version of the problem, check dimentions and units.
3
Ch.2:Basic Operations 1)Scaler Operations: we refer to a single number as a scalar, and we now introduce matlab operators for doing calculations with scalares. ^ * / \ + -
exponentiation multiplication right division left division addition subtraction
a^b a*b a/b a\b a+b a-b
a- order of precedence: Parentheses:evaluated starting with innermost pair. First Second Exponentiation:evaluated from left to right. Multiplication and divisionwith equal precedence:evaluated from left to right. Third Fourth Addition and subtraction with equal precedence:evaluated from left to right. b-complex number operations: matlab handles complex number algebra automatically. Use the symboles i and j to denote the imaginary part, where i =j=sqrt(-1).see the following operations: addition,subtraction,multiplication,division,exponentiation,and addition,subtraction,multiplication,division,ex ponentiation,and conjucate: » s=3+7i; » w=5-9i; » s+w ans = 8.0000 - 2.0000i » s*w ans = 78.0000 + 8.0000i » s/w ans = -0.4528 + 0.5849i » s\w ans = -0.8276 - 1.0690I » s^w ans = 9.1694e+008 +1.1419e+008i » (-3+7i)*(-3-7i) (-3+7i)*(-3-7i) ans = 58
2)Arrays: collections of scalars arranged in a logical structure. a- row and column columnss vect vectors ors:: a position vector cannot have more than three components,the concept of a vector can be generalized to any number of components. In matlab a vector is simply a list of scalars whose orders are xyz coordinates. » r=[2 4 10] r = 2 4 » s=[2;4;10]
10
4
s = 2 4 10
to append one vector to another: » » » c
a=[2 3 4]; b=[7 6 5]; c=[a,b] = 2 3
4
7
6
5
to create a vector x of values with a spacing q, the first value is m, the last value is n: » x=[0:2:8] x = 0 2 4 6 8 » y=[10:-.2:9] y=[10:-.2:9] y = 10.0000 9.8000 9.6000 9.4000 9.2000 9.0000 you can type the command linspace to get the same answer; so: linspace(5,8,31) is equivalent to [5:.1:8] , so the increment q is omitted. b- two-dimentional arrays: an array can have multiple rows, multiple columns, or both, called a matrix. » m=[2 3;4 5;6 7] m = 2 3 4 6 7
5
to create a matrix from vectors: » » » c
a=[1 3 5]; b=[7 9 11]; c=[a;b] = 1 3 7 9
5 11
c- matrices and the transpose operation: transpose interchanges the rows and columns: » A=[2 6;3 5]; » A' ans = 2 6 5
3
d- array addressing: which enables you to correct entries in an array without retyping the entire array. » B=[2 4 10 13;16 3 7 18;8 4 9 25;3 12 15 17]; » B(2:3,1:3) ans = 16 3 7 8 4 9 » D=B([2 2 2],:) D = 16 3 7 18 16 3 7 18 16 3 7 18
e- multidimentional arrays: we want to create a three-dimentional array whose first page is:[4 6 1;5 8 0;3 9 2]; and the second page is: [6 2 9;0 3 1;4 7 5]; to do so: » A=[4 6 1;5 8 0;3 9 2]; » A(:,:,2)=[6 2 9;0 3 1;4 7 5];
5
Matlab display: » A(:,:,1) ans = 4 5 3 » A(:,:,2) ans = 6 0 4
6 8 9
1 0 2
2 3 7
9 1 5
3)Array Operation: a- array addition and sutraction arrays must have the same size: » a=[1 2 3;4 5 6;7 8 9]; » b=[9 8 7;6 5 4;3 2 1]; » a+b ans = 10 10 10 10 10 10 10 10 10
b- exponentiation and multiplication by a scalar in array: » b=[3 6;4 1;0 9]; » c=5*b c = 15 30 20 5 0 45 » b=[3 6;4 1;0 9]; » d=b.^5 d = 243 7776 1024 1 0 59049
c- array multiplication and division: to multiply element by element, arrays must have the same size: » a=[1 2 3;4 5 6;7 8 9]; » b=[9 8 7;6 5 4;3 2 1]; » a.*b ans = 9 16 21 24 25 24 21 16 9
4)Matrix Operation: a- multiplication of vectors: vectors are simply matrices with one row or one column. To multiply two matrices A and B, the number of columns of A must equal the number of row of B.so: Cik =A =AijxB jk » A=[6 2;10 3;4 7]; » B=[9 8;5 12]; » C=A*B C 72 105 116
6
71
71
116
b- matrix exponentiation: the two matrices must be square and have the same size. » s=[1 2;3 4]; » v=s^2 v = 7 15
10 22
Example of array and matrix operations: The following table shows the hourly cost of four types of manufacturing process. It also shows the number of hours required of each process to produce three different products. Use Matlab to solve the following : a- determine determine the cost cost of of each proces processs to produce produce one unit unit of produc productt 1. b- Determine Determine the cost to to make make one unit of of each product. product. c- Suppose Suppose we produce produce 10 units units of product product 1,5 1,5 units units of product product 2, and 7 units units of product 3. Compute the total cost. Hours required to produce one unit Process hourly cost($) product 1 Printer 10 6 Screen 12 2 Scanner 14 3 Routers 9 4
product 2 product 3 5 4 3 1 2 5 0 3
Solution: ausing ar array mu multiplication: » h_cost=[10 12 14 9]; » h_1=[6 2 3 4]; » process_c_1=h_cost.*h_1 process_c_1 = 60 24 42 36
b-
using matrix multiplication:
» h_1=[6 2 3 4]; » h_2=[5 3 2 0]; » h_3=[4 1 5 3]; » u_cost=h_cost*[h_1',h_2',h_3'] u_cost = 162 114 149
c-
using matrix multiplication:
» units=[10 5 7]; » total_cost=units*u_cost' total_cost=units*u_cost' total_cost = 3233
5)Polynomials: a- polynomial algebra: to add two polynomials make sure that they have the same degree.for multiplication, use the command conv. And for division use [q,r]= deconv command: » f=[9 -5 3 7]; » g=[6 -1 2]; » conv(f,g) ans = 54 -39 41 29 -1 14 » [quotient,remainder]=de [quotient,remainder]=deconv(f,g) conv(f,g) quotient = 1.5000 -0.5833 remainder = 0 0 -0.5833 8.1667 7
the polyval(a,x) command evaluates a polynomial at specified values of its independent variable x with coefficient a. » » » f
a=[9 -5 3 7]; x=[0:2:10]; f=polyval(a,x) f=polyval(a,x) = 7
65
515
1789
4319
8537
b- poly polyno nomi mial al root roots: s: the roots of polynomial f(x) are the values of x such that f(x)=0.a polynomial with degree n has n roots. The roots can be real or complex. » a=[1 -7 40 -34]; » roots(a) ans = 3.0000 + 5.0000i 3.0000 - 5.0000i 1.0000 the poly(r) command computes the coefficients of the polynomial whose roots are spesified by the array r: » r=[1 3+5i 3-5i]; » poly(r) ans = 1 -7 40 -34
8
Ch.3:Files,Functions,and Data Structure 1)Controlling Input and Output: matlab provides several commands for obtaining input from the user and for formatting the output. a- using the disp command: to obtain a desired parallel-plate capacitor C
=
(n
1) ε A / d
−
permittivity=8.85e-12; A=20/100^2; d=4/1000; n=[2:10]; C=((n-1).*permittivity*A/d)*1e12; table(:,1)=n'; table(:,2)=C'; disp(' disp('No. plates capacitance (F) X e12') disp(table) No. plates capacitance(F) X e12 2.0000 4.4250 3.0000 8.8500 4.0000 13.2750 5.0000 17.7000 6.0000 22.1250 7.0000 26.5500 8.0000 30.9750 9.0000 35.4000 10.0000 39.8250 b- format and fprintf commands: the format command controls how numbers appear on the screen, and fprintf
command provides more control over the display format control codes: format codes: Code Description Code Description \n %e Start new line Scientific formatwith lowercase e \r %E Beginning of new line Scientific format with uppercase E \b %f Backspace Decimal format \t %g Tab %e or %f, whichever is shorter “ Apostrophe \\ Backslash » r=[2.25:20:42.25]; r=[2.25:20:42.25]; » circum=2*pi*r; circum=2*pi*r; » y=[r;circum]; y=[r;circum]; » fprintf('%5.2f fprintf('%5.2f %11.5g\n',y) 2.25 14.137 22.25 139.8 42.25 265.46 » fprintf('the first circum is %6.3f.\n',circum(1)) the first circum is 14.137.
c- user in input: the input command displays text on the screen, waits for the user to input data which stored in specified variable.for example, the command:
9
x=input(‘please enter the value of x:’) causes the message to appear on the
screen.
2)Elementary Mathematical Functions: the following table shows the functions that are relevant to your application: Exp;Log;Complex Trigonometric I.trigonometric Hyperbolic I.hyperbolic Exp(x) Log(x) Abs(x) Angle(x) Conj(x) Imag(x) Real(x)
cos(x) sin(x) tan(x) sec(x) csc(x) cot(x) --
acos(x) asin(x) atan(x) asec(x) acsc(x) acot(x) --
cosh(x) sinh(x) tanh(x) sech(x) csch(x) coth(x) --
acosh(x) asinh(x) atanh(x) asech(x) acsch(x) acoth(x) --
3)Cell Arrays: we can create cell array by using assignment statement or cell function. We can assign data to the cell by using either cell indexing or content indexing. To use cell indexing, enclose in parentheses the cell subscripts on the left of the assignment statement and use the standard array notation.enclose the cell contents on the right side of the assignment statement in braces {}. To build database of 2x2 cell array contains location,date,air temp., and water temp. as follows: Hebron June 13, 1997 30 42 35 25 27 26 24 26 25 22 25 23 » » » » » » » » » » » A
A(1,1)={'Hebron'}; A(1,1)={'Hebron'}; A(1,2)={'June 13,1997'}; A(2,1)={[30 42 35]}; A(2,2)={[25 27 26;24 26 25;22 25 23]}; %use the content indexing: A{1,1}='Hebron'; A{1,1}='Hebron'; A{1,2}='June 13,1997'; A{2,1}=[30 42 35]; A{2,2}=[25 27 26;24 26 25;22 25 23]; %type A: A = 'Hebron' 'June 13,1997' [1x3 double] [3x3 double] » %Use celldisp function to display the full contents of A: » celldisp(A) A{1,1} = Hebron A{2,1} = 30 42 35 A{1,2} = June 13,1997 A{2,2} = 25 27 26 24 26 25 22 25 23
10
4)Structure Arrays: the structure enable you to store dissimilar arrays together. We want to create a structure array to contain the following types of student data: -student number. -student name. -student address. -test score. S.number S.name S.address Test score Ahmad 98-1456 Ramallah 83 Amjad 99-7203 Jinen 78 » S(1).number='98-1456'; S(1).number='98-1456'; » S(1).name='ahmad'; S(1).name='ahmad'; » S(1).address='ramallah' S(1).address='ramallah'; ; » S(1).test='83'; S(1).test='83'; » S(2).number='99-7203'; S(2).number='99-7203'; » S(2).name='amjad'; S(2).name='amjad'; » S(2).address='jinen'; S(2).address='jinen'; » S(2).test='78'; S(2).test='78'; » S S = 1x2 struct array with fields: number name address test » S(1) ans = number: '98-1456' name: 'ahmad' address: 'ramallah' test: '83' » size(S) ans = 1 2 » length(S) ans = 2 » %TO obtain information about the fields: » fieldnames(S) fieldnames(S) ans = 'number' 'name' 'address' 'test'
Also, we can create a structure array with fields as follows: »S=struct('number','vnumber','name','vname','address','vaddress','test', 'vtest') S = number: 'vnumber' name: 'vname' address: 'vaddress' test: 'vtest'
11
Ch.4:Plotting with Matlab 1)xy Plotting Function: plot,label, title,grid,and axis commands: a- plot,label, » x=[0:.1:52]; x=[0:.1:52]; » y=.4*sqrt(1.8*x); y=.4*sqrt(1.8*x); » plot(x,y),xlabel('distance(miles)'), ylabel('hieght(miles)'),… title('rocket hieghtas a function of drawing distance'),… grid on, axis([0 52 0 5]) ro c k e th ie g h ta s afu n c tio n o fd ra w in g d is ta n c e 5 4 .5 4
) 3.5 s e 3 l i m ( t h 2.5 g e i h 2 1 .5 1 0 .5 0 0
5
1 0
1 5
2 0 2 5 d is ta n c e ( m ile s )
3 0
3 5
4 0
4 5
5 0
b- plotting polynomial: use polyval function to plot the polynomial » x=[-6:.1:6]; x=[-6:.1:6]; » p=[3 2 -100 2 -7 90]; » plot(x,polyval(p,x)),xl plot(x,polyval(p,x)),xlabel('x'),y abel('x'),ylabel('p') label('p')
5000 4000 3000 2000 000 p 1 0 -100 0 -200 0 -300 0
-6
-4
-2
0 x
12
2
4
6
2)Subplots: use subplot command to obtain several plots on the same figure. » » » » » » » »
x=[0:.1:5]; y=exp(-1.2*x).*sin(10*x y=exp(-1.2*x).*sin(10*x+5); +5); subplot(1,2,1); subplot(1,2,1); plot(x,y),xlabel('x'),y plot(x,y),xlabel('x'),ylabel('y'), label('y'),axis([1 axis([1 5 -1 1]) x=[-6:.1:6]; x=[-6:.1:6]; y=abs(x.^3-100); y=abs(x.^3-100); subplot(1,2,2); subplot(1,2,2); plot(x,y),xlabel('x'),ylabel('y'),axis([-6 6 0 350])
1
350
0.8 300 0.6 250
0.4 0.2 y
200
0
y
150
-0.2 -0.4
100
-0.6 50 -0.8 -1
1
2
3 x
4
5
0
-5
0 x
5
3)Data Markers and Line Types: Data Markers Dot: (.) Asterisk: (*) Cross: ( x) Plus sign: (+) Circle: (o) -use small circle:
Line Types Solid line ( -) Dashed line ( - -) Dashed-dotted line(-.) Dotted line (:)
Colors Black: Blue Cyan Green Magenta
» plot(x,y,'o'); plot(x,y,'o');
-plot data twice: » plot(x,y,x,y,'o'); plot(x,y,x,y,'o'); -plot x,y with solid line and u,v with dashed line: » plot(x,y,u,v,'--'); plot(x,y,u,v,'--'); -plot x,y with(*)connected with(:): » plot(x,y,'*',x,y,':'); plot(x,y,'*',x,y,':'); -plot x,y with green(*) connected with a red(:): » plot(x,y,'g*',x,y,'r--' plot(x,y,'g*',x,y,'r--'); );
4)Labeling Curves and Data: to distinguish between more than one plot on the same graph: a- use legend command: » x=[0:2:20];
13
k b c g m
Colors Red White Yellow
r w y
» y=exp(x); » z=2*tan(x); » plot(x,y,x,z,'--'),lege plot(x,y,x,z,'--'),legend('exp(x)' nd('exp(x)','2*tan(x)') ,'2*tan(x)')
5
x 10
8
exp(x) 2*tan(x) 4
3
2
1
0
-1
0
2
4
6
8
10
12
14
16
18
20
b- use text and gtext commands: » » » »
x=[0:2:20]; y=exp(x); z=2*tan(x); plot(x,y,x,z),gtext('exp(x)'),text(10,18,'2*tan(x)') 5
x10
8
4 exp(x) 3
2
1
0
-1
0
2*tan(x)
2
4
6
8
5)Logarithmic Plots: » RC=.1; » s=[1:100]*i; s=[1:100]*i; » M=abs(1./(RC*s+1)); M=abs(1./(RC*s+1)); » loglog(imag(s),M),grid, loglog(imag(s),M),grid,... ... xlabel('freq.'),ylabel('responce')
14
10
12
14
16
18
20
0
1 0
e c n o p s e r
-1
1 0
0
1 0
1 0 fr e q .
1
2
1 0
6)Controlling Tick-Mark Spacing and Labels: to plot y=0.25x^2 for 0<=x<=2, with tick marks spaced at interval of 0.2: » x=[0:.2:2]; » y=.25*x.^2; » plot(x,y),set(gca,'XTick',[0:.2:2],'YTick',[0:.1:1]); 1 0 .9 0 .8 0 .7 0 .6 0 .5 0 .4 0 .3 0 .2 0 .1 0 0
0 .2
0 .4
0 .6
0 .8
1
1 .2
1 .4
1 .6
1 .8
2
7)Stem,Stars, and Bar Plots: » x=[1:6]; » y=[13 5 7 14 10 12]; » stem(x,y),... stem(x,y),... set(gca,'XTicklabel',['jan';'feb';'mar';'apr';'may';'jun']),... set(gca,'XTick',[1:6]),axis([1 5 0 15]);
15
10
5
0
jan
feb
15
m ar
apr
m ay
8)Polar Plots: » theta=[0:pi/90:2*pi]; theta=[0:pi/90:2*pi]; » r=2./(1-.5*cos(theta)); r=2./(1-.5*cos(theta)); » polar(theta,r) polar(theta,r)
90
4
120
60 3 2
150
30
1
180
0
210
330
240
300 270
9)Three-Dimentional Line Plots: a- plot3 to plot: x
=
y z
e
=
=
e
0.05 t
−
0.05 t
−
sin t cos t
t
» t=[0:pi/50:10*pi]; t=[0:pi/50:10*pi]; » plot3(exp(-.05*t).*sin(t),exp(-.05*t).*cos(t),t),... xlabel('x'),ylabel('y'),zlabel('z'),grid
40
30
z
20
10
0 1 0.5
1 0.5
0
0
-0.5 y
-0.5 -1
16
-1
x
b- surface mesh plots: to plot: z = xe xe
2 2 2 [( x − y ) + y ]
−
for –2<=x<=2 and –2<=y<=2; » [x,y]=meshgrid(-2:.1:2) [x,y]=meshgrid(-2:.1:2); ; » z=x.*exp(-((x-y.^2).^2+ z=x.*exp(-((x-y.^2).^2+y.^2)); y.^2)); » mesh(x,y,z); mesh(x,y,z);
c- contour plots: to plot: z = xe xe
2
[( x − y )
−
2
2
y ]
+
for-2<=x<=2 and –2<=y<=2; » [x,y]=meshgrid(-2:.1:2) [x,y]=meshgrid(-2:.1:2); ; » z=x.*exp(-((x-y.^2).^2+ z=x.*exp(-((x-y.^2).^2+y.^2)); y.^2)); » contour(x,y,z) contour(x,y,z)
2 1.5 1 0.5 0 -0.5 -1 -1.5 -2 -2
-1.5
-1
-0.5
17
0
0.5
1
1.5
2
10) Graphical Solution of Equations: when we need to solve two equations in two unknown variables, we can plot the equations. The solution corresponds to the intersection of the two lines. If they do not intersect, there is no solution. Example of graphical solution equations: The following figure is a representation of an electrical system with a power supply and a load. The power supply produces the fixed voltage v1 and supplies the current i1 required by the load, whose voltage drop is v2. The current-voltage relationship for a specific load is found from the experiment to be: i1= 0.16*(exp(0.12v2)-1 ……………….(1)_ suppose that R1 =30Ω and the supply voltage is v1=15v. to select or design a power supply, we need to determine how much current will be drawn from the power supply when this load is attached. Find the voltage drop v2 as well.
Solution: Using kirchhoff’s voltage law, we obtain: V1-i1R1-v2=0 Solve for i1 to obtai load line: I1=-v2/R +v1/R …………………………..(2) To find i1,v2, we need to solve the eq. (1), (2): Because of term exp(0.12v2), it is not possible to obtai a solution using algebra. However, we can plot the curves corresponding to these equations and find their intersection. » v_2=[0:0.01:20]; v_2=[0:0.01:20]; » i_11=.16*(exp(.12*v_2)i_11=.16*(exp(.12*v_2)-1); 1); » i_12=-(1/30)*v_2+.5; i_12=-(1/30)*v_2+.5; » plot(v_2,i_11,v_2,i_12),grid,xlabel('v_2(volts)'),... ylabel('i_1(amperes)'),axis([0 20 0 1]),... gtext('load line'),gtext('device curve')
from the figure we can see that the curves intersect at approximately i1=0.25 amperes, v2=7.5 volts. 1 0 .9 0 .8 d e v ic e cu r v e
0 .7
) s 0.6 e r e p 0.5 m a ( i 10.4
lo a dlin e
0 .3 0 .2 0 .1 0 0
2
4
6
8
1 0 v (v o lts) 2
18
1 2
1 4
1 6
1 8
2 0
11) Function Discovery: Is the process of finding a function that can describe a particular set of data. We will deal with three types of functions : a- the linear function: y(x) = mx + b note that y(0) = b. b b- the the powe powerr func functi tion on:: y(x) = bx^m note that y(0) = 0 if m >= 0, and y(0) = ∞ if m < 0. c- the the expon exponen enti tial al func functi tion on:: y(x) = b(10)^mx or its equivalent form: y = b*exp(mx) where (exp) is the base of the natural logarithm (ln exp = 1). Each function gives a straight line when plotted using a specific set of axis: a- the linear linear function function gives gives a straight straight line line when plotted plotted on rectilinear rectilinear axis. axis. b- the power power function function gives gives a straight straight line line when plotted plotted on on log-log log-log axis. axis. c- the exponenti exponential al function function gives a straight straight line line when plotted plotted on semilog semilog plot plot whose y-axis is logarithmic. In this process we will use polyfit command to determine the type of function. The syntax of polyfit command for the three types of functions is as follows: a- Linear function: y = mx+b. in this case the variables w and z in polynomial w = p1z+p2 are the original data variables x and y, and we can find the linear function that fits the data by typing p = polyfit(x,y,1), where p1 is m, and p2 is b. b- Power function: y = bx^m, so log10y = mlog10x + log10b which has the form: w=p1z+p2, where the polynomial variables w and z are related to the origin data variables x and y by w=log10y and z=log10x. thus we can find the power function that fits the data by typing: p=polyfit(log10(x),log10(y),1) , where p1 is m and p2 is log10 b. we can find b from b=10^p2. c- Exponential function: y = b(10)^mx. In this case log10 y=mx+log10 b, which has the form: w=p1z+p2, where the polynomial variables w and z are related to the origin data variables x and y by w=log10y and z=x. thus we can find the exponential function that fits the data by typing: p=polyfit(x,log10(y),1), where p1 is m and p2 is log10 b. we can find b from b=10^p2. Example of function discovery: Use function discovery techniques to find the functional relation between F and X: X Y
0 0
100 100 0.09 0.09
200 200 0.18 0.18
300 300 0.28 0.28
400 400 0.37 0.37
500 500 0.46 0.46
600 600 0.55 0.55
700 700 0.65 0.65
800 800 0.74 0.74
Solution: » Y=[0 .09 .18 .28 .37 .46 .55 .65 .74]; » X=[0:100:800]; X=[0:100:800]; » plot(X,Y) 0 . 8
The plot appears as a straight line, so we can describe the data by the relation: Y=mX+b, but b is zero, because we started from the origin. » p=polyfit(X,Y,1); p=polyfit(X,Y,1); » m=p(1)
0 . 7
0 . 6
0 . 5
0 . 4
0 . 3
0 . 2
0 . 1
0 0
19
1 0 0
2 0 0
3 0 0
4 0 0
5 0 0
6 0 0
7 0 0
8 0 0
m = 9.2667e-004
Ch.5:Linear Algebraic Equations 1)Elementary Solution Method: a- singular and nonsingular: a singular problem refers to a set of equations having either no unique solution or no solution at all. 3x-4y=5 6x-8y=10 b- homogeneous equations: equations with ritht sides are all zeros. 6x+ay=0 2x+4y=0
2)Matrix Methods for Linear Equations: to find the xi solutions in matlab: Ax=b Find the matrix inverse:
A'Ax=A'b x=A'b if the determinant of matrix is equal to zero, then there is no solution. So; to solve the linear equations: 2x1+9x2=5 3x1-4x2=7 » A=[2 9;3 -4]; » b=[5;7]; » det(A) ans = -35 » x=inv(A)*b x = 2.3714 0.0286
the inverse matrix method works only if the matrix A is square and non-singular (|A| does not equal zero). Another one is the left-division method, which can be used if the matrix is not square. » x=A\b x = 2.3714 0.0286
3) Underdetermined Systems: the underdetermined system does not contain enough information to solve for all of the unknoun unknoun variables. if the matrix A is singular, then we can’t use the inverse and left-division metods. So, to obtain a normal solution, we use the command pinv. So: x=pinv(A)*b.
20
a- matrix rank An mxn matrix A has a rank r>=1if and only if |A| contains a nonzero rxr determinant and every square subdeterminant with r+1 or more rows is zero. » A=[3 -4 1;6 10 2;9 -7 3]; » rank(A) ans = 2
b- existence and uniqueness of solution the following test determines whether or not a solution exists and unique. [A b]: augmented matrix. The set Ax=b with m equations and n unknouns has solution if and only if rank[A]=rank[A b]. » A=[2 -4 5;-4 -2 3;2 6 -8]; » b=[-4;4;0]; » rank(A) ans = 2 » rank([A b]) ans = 2
4)Overdetermined System: overdetermined system is a set of equations that has more independent equations than unknouns. 0x+y=2 5x+y=6 10x+y=11 » A=[0 1 5 1 10 1]; » A=[0 1;5 1;10 1]; » b=[2;6;11]; » rank(A) ans = 2 » rank([A b]) ans = 3 » A\b ans = 0.9000 1.8333
21
Ch.6:Programming With Matlab 1)Relational and Logical Operations: Rela Relatio tiona nall opera operati tion onss Mean Meanin ing g < Less than <= Less than or equal to > Greater than >= Greater than or equal to = = Equal to ~= Not equal to Logical operations ~ & | Xor
Name NOT AND OR Exclusive OR
Order of precedence for operator types: Prec Preced eden ence ce Op Oper erat ator or type type Parentheses:evaluated starting with the innermost pair. First Arithmetic operators and NOT(~):evaluated from left to right. Second Relational operators:evaluated from left to right. Third Logical operators: evaluated from left to right. Fourth
See also: » » » a
x=[6 3 9]; y=[14 2 9]; a=(x
0
1
1
Truth Table: » X=[1 1 0 0]'; » Y=[1 0 1 0]'; » Truth_table=[X,Y,~Y,X|Y,X&Y,xor(X,Y)] Truth_table = 1 1 0 1 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 0 1 0 0 0
22
2)Conditional Statements: the if statement the basic form is: if logical expression statements end
a-
» if x>=0 y=sqrt(x) end
nested if statement the basic form is: if logical expression 1 statement group 1 if logical expression2 statement group2 end end c-the else statement the basic form is: if logical expression statement group1 else statement group2 end b-
» x=[3 7 -2]; » if x>=0 y=sqrt(x) else y=exp(x) end y = 1.0e+003 * 0.0201 1.0966 d- the elseif statement
0.0001
the basic form is: if logical expression1 statement group1 elseif logical expression2 statement group2 end » x=[-1 6 4]; » if x>6 y=x^2 elseif x<=6 y=x-2 end y = -3 4
2
23
another example: » x=[-4:2:4]; » if x>4 y=log(x) elseif x>=0 y=sqrt(x) else y=exp(x) end y = 0.0183 0.1353
1.0000
7.3891
54.5982
3)Loops: a- for loop the basic form:
for loop variable=m:s:n statements end the expression m:s:n assigns an initial value of m incremented by s to the value of n. » %outer loop: » m=0; » for q=0:3:6 m=m+1 %inner loop: n=0; for r=0:5:15 n=n+1; A(m,n)=r+q; end end A = 0 5 3 8 6 11
10 13 16
15 18 21
b- While Loop: the basic form:
while logical expression statements end » x=5; » while x<30 disp(x) x=2*x; end 5 10 20
24
Ch.7:Statistics and Interpolation 1)Statistics and Histogram: to obtain the histogram for two scores of a class: the first scores were: 61 61 65 67 69 72 74 74 76 77 83 83 85 88 89 92 93 93 95 98 and the second scores: 66 69 72 74 75 76 77 78 78 79 79 80 81 83 84 85 87 88 90 94 t1=[5 5 5 5]; t2=[2 9 7 2]; x=[64.5 74.5 84.5 94.5]; subplot(2,1,1) bar(x,t1),axis([60 bar(x,t1),axis([60 100 0 10]),... 10]),... title('histogram title('histogram of scores for t1'), t1' ),... ... xlabel('score xlabel( 'score range'),ylabel( range'),ylabel('number 'number of scores') scores') subplot(2,1,2) bar(x,t2),axis([60 bar(x,t2),axis([60 100 0 10]),... 10]),... title('histogram title('histogram of scores for t2'), t2' ),... ... xlabel('score xlabel( 'score range'),ylabel( range'),ylabel('number 'number of scores') scores') s e 0 r 1 o c s 8 f o 6 r e b 4 m u n 2 0 6 0
h is to g r a m o fs c o r e s fo rt1
6 5
7 0
6 5
7 0
7 5
8 0 s c o r e r a n g e h is to g r a m o fs c o r e s fo rt2
s e 0 r 1 o c s 8 f o 6 r e b 4 m u n 2 0 6 0
7 5
8 0 s c o r e r a n g e
8 5
9 0
9 5
1 0 0
8 5
9 0
9 5
1 0 0
Example of using histogram to find the absolute frequency: y=[92 94 93 96 93 94 95 96 91 93 95 95 95 92 93 94 91 94 92 93]; x=[91:96]; hist(y,x),axis([90 97 0 6]) 6
5
4
3
2
1
0 9 1
9 2
25
9 3
9 4
9 5
9 6
2)Normal Distribution: -absolute frequency:number of times a particular outcome occurs. -relative frequency: number of times a particular outcome occurs divided by the whole number of outcomes. -scaled frequency histogram: the absolute frequency histogram divided by the total area of that histogram which equal 1. %absolute frequency data. y_abs=[1,0,0,0,2,4,5,4,8,11,12,10,9,8,7,5,4,4,3,1,1,0,1]; binwidth=.5; %compute scaled frequency data. area=binwidth*sum(y_abs); y_scaled=y_abs/area; %define the bins. bins=[64:binwidth:75]; %plot the scaled histogram. bar(bins,y_scaled),... bar(bins,y_scaled), ... ylabel('scaled ylabel( 'scaled frequency'),xlabel( frequency'),xlabel('hieght(inches)' 'hieght(inches)') ) 0.25
0.2 y c n e 0.15 u q e r f d e l a c s
0.1
0.05
0 62
64
66
68 70 hiegh t(in inch es)
72
74
76
-mean:the average of outcomes.(µ) -median:the middle value of data if the number of data is odd. -Standard deviation: describe the width and spread of the normal distribution curve.(σ) -variance: describe the sprea of curve. P ( x )
=
1 σ (2π )
0.5
e
−(
2 2 x − µ ) / 2σ
to compute the probability at other points, use erf function which return the area to the left of the value t=x. mu=69.3; sigma=1.96; %how many are no taller than 68 inches? b1=68; p1=(1+erf(b1-mu)/(sigma*sqrt(2)))/2 %how many are within 3 inches of the mean? a2=66.3; b2=72.3; p2=(erf((b2-mu)/sigma*sqrt(2)))-erf((a2-mu)/(sigma*sqrt(2)))/2
26
3)Interpolation: to estimate a variables value between the data points. a- linear interpolation: x=[7 9 11 12]; y=[49 57 71 75]; x_int=[8 10 11.5]; interp1(x,y,x_int)
b- 2-D interpolation: find the temprature at the point(0.6,1.5): » » » »
x=[0 1]; y=[0 2]; z=[49 54;53 57]; interp2(x,y,z,.6,1.5) interp2(x,y,z,.6,1.5)
27
Ch.8:Numerical Methods for Calculus and Diffrential Equations 1)Numerical Integration: to find the area under the curve, we will implement trpezoidal integration with trapz function. » x=linspace(0,pi,10); x=linspace(0,pi,10); » y=sin(x); » trapz(x,y) ans = 1.9797 the quad and quad8 functions are more accurate than trapz; » A=quad('sin',0,10) A=quad('sin',0,10) A = 1.8391
2)Numerical Differentiation: a- the diff(x) function returns the differences between adjacent elements in x. » x=[5 7 6 9 2]; » y=diff(x) y = 2 -1 3 -7 b- the polyder function returns the derivative of polynomial. » p=[3 6 0 2 1]; » polyder(p) ans = 12 18 0 2
c- backward, backward, forward, forward, and cent central ral differenc difference: e: y 2 − y1 y 2 − y1 = sb = x 2 − x1 ∆ x y3 − y 2 y3 − y 2 sf = = x3 − x 2 ∆ x sc
=
sb + sf
2
=
1 y 2 − y1 y3 − y 2 + ( ) ∆ x ∆ x 2
=
y3 − y1
2∆ x
Example of comparison between Backward and Central Difference: Use uniformly distributed error between –0.025 and +0.025 to estimate the difference between backward and central difference of y=sin(x) that is measured 51 times. x=[0:pi/50:pi] n=length(x) td=cos(x)% true derivative % generating function with +/-0.025 random error. y=sin(x)+.05*(rand(1,51)-.5) % backward difference. d1=diff(y)./diff(x) subplot(2,1,1) plot(x(2:n),td(2:n),x(2:n),d1, 'o' 'o'),xlabel(‘x’),… ),xlabel(‘x’),… ylabel(‘derivative’),ax ylabel(‘derivative’),axis([0 is([0 pi -2 2]),… gtext(‘backwarb difference’) 28
%central difference. d2=(y(3:n)-y(1:n-2))./(x(3:n)-x(1:n-2)) subplot(2,1,2) plot(x(2:n-1),td(2:n-1),x(2:n-1),d2, 'o' 'o'),xlabel(‘x’),… ),xlabel(‘x’),… ylabel(‘direvative’),ax ylabel(‘direvative’),axis([0 is([0 pi -2 2]),… gtext(‘central gtext(‘central difference’)
2 backwarb difference ifference 1 e v i t a v i r e d
0
-1
-2 0
0.5
1
1.5
2
2.5
3
x 2 central differen difference
1 e v i t a v e r i d
0
-1
-2 0
0.5
1
1.5
2
2.5
3
x
3)Numerical Methods for Differential Equations: a- the the eule eulerr meth method od:: if y’=-10y; y(0)2;Δt=0.02; then y(t)=2exp(-10*t) find the solution over the range 0<=t<=0.5; r=-10; delta=.02; y(1)=2; k=0; for time=[delta:delta:.5] k=k+1; y(k+1)=y(k)+r*y(k)*delta; end t=[0:delta:.5]; y_true=2*exp(-10*t); plot(t,y,’o’,t,y_true),xlabel( 't' 't'),ylabel( ),ylabel('y' 'y') ) 2 1 .8 1 .6 1 .4 1 .2
y
1 0 .8 0 .6 0 .4 0 .2 0 0
0 .0 5
0 .1
0 .1 5
0 .2
29
0 .2 5 t
0 .3
0 .3 5
0 .4
0 .4 5
0 .5
b- the pred predictor ictor-corr -correcto ectorr method: method: for the same example: r=-10; delta=.02; y(1)=2; k=0; for time=[delta:delta:.5] k=k+1; x(k+1)=y(k)+delta*r*y(k); y(k+1)=y(k)+(delta/2)*(r*y(k)+r*x(k+1)); end t=[0:delta:.5]; y_true=2*exp(-10*t); plot(t,y,'o' plot(t,y,'o',t,y_true),xlabel( ,t,y_true),xlabel( 't' 't'),ylabel( ),ylabel('y' 'y') )
2 1.8 1.6 1.4 1.2
y
1 0.8 0.6 0.4 0.2 0 0
0.05
0.1
0.15
0.2
0.25 t
30
0.3
0.35
0.4
0.45
0.5
Ch.9:Symbolic Processing With Matlab 1) Symboli Symbolicc Expressio Expressions ns and Algeb Algebra: ra: a- use the function sym to create a symbolic constants by using a numerical value for the argument: » pi=sym('pi') pi = pi
b- use the function syms to create a symbolic variables or symbolic matrices without arguments: » syms x y real » s=x+y; » r=sqrt(x^2+y^2) r = (x^2+y^2)^(1/2)
another example: » n=3; » syms x; » A=x.^((0:n)'*(0:n)) A = [ 1, 1, 1, 1] [ 1, x, x^2, x^3] [ 1, x^2, x^4, x^6] [ 1, x^3, x^6, x^9] c- use the function findsym(E) to determine the symbolic variable in particular expression E, and findsym(E,n) returns the n symbolic variables in E closest to x: » syms b x1 y » findsym(6*b+y) findsym(6*b+y) ans = b, y » findsym(6*b+y+x1,2) ans = x1,y
d-use the functions: *collect(E) collscts coefficients of like powers in expression E. *expand(E) expand the expression E. *factor(E) factors the expression E. *simplify(E) simplifies the expression E. *[num,den]=numden(E) returns numerator and denominator of E. *double(E) converts the expression E to numeric form. *poly2sym(E) converts a coefficient vector E to a symbolic polynomial. *prod(m:n) compute numeric factorial. *class(E) returns the class of E. *ezplot(E) plot expression E.
2) Algebraic Algebraic and Transcendental Transcendental Equations: Equations: Use the function solve(E) to solve the symbolic expression E » eq='x+5=0'; » solve(eq) ans = -5 or:
31
» solve('x+5=0') ans = -5
another example: » eq1='6*x+2*y=14'; » eq2='3*x+7*y=31'; » [x,y]=solve(eq1,eq2) [x,y]=solve(eq1,eq2) x = 1 y = 4
3)Calculus: a- diffrentiation: » syms n x; » diff((sin(x))^n) diff((sin(x))^n) ans = sin(x)^n*n*cos(x)/sin(x) » simplify(ans) ans = sin(x)^(n-1)*n*cos(x)
another example: » diff((sin(x))^n,2) diff((sin(x))^n,2) ans = sin(x)^n*n^2*cos(x)^2/sin(x)^2-sin(x)^n*n-sin(x)^n*n*cos(x)^2/sin(x)^2
another one: » diff((sin(x))^n,n,2) diff((sin(x))^n,n,2) ans = sin(x)^n*log(sin(x))^2
b- integration: » syms n x; » int(x^n) ans = x^(n+1)/(n+1)
another example: » syms n x; » int(x^n,2,5) int(x^n,2,5) ans = (5*5^n-2*2^n)/(n+1)
another one: » syms n x; » int(x^n,n,2,5) int(x^n,n,2,5) ans = x^2*(x^3-1)/log(x)
c- tayl taylor or seri series es:: taylor’s theorem states that a function f(x) can be represented in the vicinity of x=a by the expantion: 1 d 2 f 1 d n f df 2 ( x − a ) n f ( x ) = f ( a ) + ( x − a) + 2 ( x − a) + .... + n 2 dx x =a n! dx x =a dx x =a » syms x » f=exp(x); » taylor(f,4) ans = 1+x+1/2*x^2+1/6*x^3 » taylor(f,3,2) ans =
32
exp(2)+exp(2)*(x-2)+1/2*exp(2)*(x-2)^2
d- sums: x −1
∑ E ( x) = E (0) + E (1) + ....... + E ( x −1)
x =0
the symsum function returns the symbolic summation of the expression E, that is » syms n k » symsum(k,0,10) symsum(k,0,10) ans = 55 » symsum(k,0,n-1) ans = 1/2*n^2-1/2*n
e- limits: the function limit(E,a) returns the limit of E as x goes to a. lim lim E ( x)
x →a
» syms h x » limit((x-3)/(x^2-9),3) limit((x-3)/(x^2-9),3) ans = 1/6
another example: » syms h x » limit((sin(x+h)-sin(x)) limit((sin(x+h)-sin(x))/h,h,0) /h,h,0) ans = cos(x)
4)Differential Equations: a- use dsolve function for solving a single equation: so, if we want to solve: dy
+ 2y dt » dsolve('Dy+2*y=12') dsolve('Dy+2*y=12') ans = 6+exp(-2*t)*C1
= 12
for the second-order equation: » dsolve('D2y=c^2*y') dsolve('D2y=c^2*y') ans = C1*sinh(c*t)+C2*cosh(c*t)
b-solving sets of equations: dx dt dy dt
=
3 x + 4 y 4 x + 3 y
=−
» [x,y]=dsolve('Dx=3*x+4* [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x y','Dy=-4*x+3*y') +3*y') x = exp(3*t)*(cos(4*t)*C1+sin(4*t)*C2) y = -exp(3*t)*(sin(4*t)*C1-cos(4*t)*C2)
e- specifyin specifying g initial initial and boundary boundary conditions conditions:: solve: (dy/dt)=sin(bt), y(0)=0; » dsolve('Dy=sin(b*t)','y dsolve('Dy=sin(b*t)','y(0)=0') (0)=0') ans = -cos(b*t)+1)/b
33
5)Laplace Transforms: » syms s t » laplace(t^3) laplace(t^3) ans = 6/s^4 » ilaplace(6/s^4) ans = t^3 » syms s b t » laplace(exp(-b*t)) laplace(exp(-b*t)) ans = 1/(s+b) » ilaplace(1/(s+b)) ans = exp(-b*t)
34
Questions: Chapter.1 1-Use matlab to obtain a labeled plot on the screen of the function y=10 sin 3x-4; for 0<=x<=5.read a point of the plot and check the values with a calculator. 2-with what initial speed must you throw a ball vertically for it to reach a height of 20 feet? The ball weighs 1 pound. Hoe does your answer change if the ball weighs 2 pounds?
Chapter.2 1-suppose that x=2 and y=5. Use matlab to compute the following: a- (yx (yx^3)/ ^3)/(x (x-y -y)) b b- (1-( (1-(1/ 1/x^ x^5) 5))^ )^-1 -1 2-use matlab to create an array x having six values between 0 and 10 (including the endpoints 0 and 10). Create an array A whose first row contains the values 3x and whose second row contains the values 5x-20. 3-type this matrix in matlab and use it to answer the following questions: A= [3
7
-4
12;-5
9
10
2;6 13
8
11;15
5
4
1]
a- create a 4x3 4x3 array B consis consisting ting of all all elements elements in the second second through through fourth fourth columns of A. b- create a 3x4 3x4 array C consis consisting ting of all all elements elements in the second second through through fourth fourth rows of A. c- create a 2x3 2x3 array D consis consisting ting of all all elements elements in the the first two two rows and and the last three columns of A. 4-compute the length and absolute value of the following vectors: a- x=[2 =[2 4 7] b- y=[2 =[2 –4 –4 7] 7] c- z=[5+3 z=[5+3i i -3+4i -3+4i
2-7i] 2-7i]
5-given the matrices: A=[64 32;24 –16] B=[16 –4;6 –2]
Use matlab to: a-find the result of A times B using the array product. b-find the result of A divided by B using array right division. c-find B raised to the third power element by element . 6-a company must purchase five kinds of material. The following table gives the price the company pays per ton for each material, along with the number of tons purchased in the months of may, june, and july: Quantity purchased (tons) material Price($/ton) 1 300 2 550 3 400 4 250 5 500
may 5 3 6 3 2
ju n e 4 2 5 5 4
july 6 4 3 4 3
Use matlab to answer these questions: a- create a 5x3 5x3 matrix matrix contain containing ing the amounts amounts spent spent on each item item for each each month. b- What is is the total spent in may? may? In june? june? In july? july?
35
c- What is the total total spent spent on each each material material in the the three-mont three-month h period? period? d- What is the total total spent spent on all all materials materials in the the three-mont three-month h period? period? 7-the following table show the costs associated with a certain product and the production volume for the four quarters of the business year. Use matlab to find: a- the quarterl quarterly y costs costs for for materials, materials, labor, and transpo transportatio rtation. n. b- The total total material, material, labor, labor, and and transport transportation ation costs costs for for the year. year. c- The The tota totall quart quarter erly ly cost costs. s. Product 1 2 3 4 5
Unit product costs(1000$) Materials Labor Trans. 7 3 2 3 1 3 9 4 5 2 5 4 6 2 1
Quarterly production volume Product Q1 Q2 Q3 Q4 1 16 14 10 12 2 12 15 11 13 3 8 9 7 11 4 14 13 15 17 5 13 16 12 18
8-use matlab to find: a- (10x^3 (10x^3-9x -9x^2^2-6x+ 6x+12) 12)(5x (5x^3^3-4x^ 4x^2-1 2-12x+ 2x+8) 8) b- (14x^3 (14x^3-6x -6x^2+ ^2+3x 3x+9)/ +9)/(5x (5x^2+ ^2+7x-4 7x-4)) c- roots roots of(13x of(13x^3+ ^3+18 182x^ 2x^2-1 2-184x 84x+25 +2503= 03=0) 0) d- polynomia polynomiall whose whose roors are: 3+6I, 3+6I, 3-6I, 3-6I, 8, 8, and 20
Chapter.3 1-let x=-5-8i and y=10-5i, use matlab to compute: a- the magnit magnitude ude and angle angle of xy. b- the magnit magnitude ude and angle angle of of x/y. x/y. 2-compute and display the 11 values of x and z in a two-column table, where z=y^3-3y^2+4y+10 y=cos x x=0,0.1,0.2,…,1. 3-create a structure array that contains the following information fields concerning the road bridges in a town: bridge location, maximum load (tons), year built, year due for maintenance. Then enter the following data into the array: Location Hebron Nablus Jinen Gaza
Max load 80 90 85 100
Year built 1928 1950 1933 1960
Due maintenance 1997 1998 1998 1999
Chapter.4 1-find the real roots of the polynomial equation: 4x^5+3x^4-95x^3+5x^2-10x+80=0 in the range –10<=x<=10 by plotting the polynomial. 2-confirm the identity tan(2x)=2tan x/(1-tan^2 x) by plotting both the left and the right sides versus x over the range 0<=x<=2π. 3-when a constant voltage was applied to a certain motor initially at rest, its rotational speed s(t) versus time was measured. The data appears in the following table:
Time(sec) Speed( Spe ed(rpm rpm))
1 1210 1210
2 1866 1866
3 2301 2301
4 2564 2564
5 2724 2724
6 2881 2881
7 2879 2879
8 2915 2915
9 3010 3010
Determine whether the following function can describe the data. If so, find the values of the constants b and c. S(t)= b(1-exp(ct)).
36
4-the following table shows the average temprature for each year in Hebron. Plot the data as a stem plot, bar plot, and stairs plot. Year Temp Temprratur aturee(ºC) (ºC)
1990 18
1991 19
1992 21
1993 17
19 1994 20
1995 22
5-in each of the following problems, determine the best function y(x) (linear, exponential, or power function) to describe the data. Plot the function on the same plot with the data: aX Y
25 0
30 250
35 500
40 750
45 1000
X Y
2.5 2.5 3 3.5 3.5 4 4.5 4.5 1500 1500 1220 1220 1050 1050 915 810 810
X Y
550 41. 41.2
b5 5.5 745 745 690 690
6 7 8 9 620 620 520 520 480 480 410 410
10 390 390
c600 18.6 18.62 2
650 8.62 8.62
700 3.92 3.92
750 1.86 1.86
6-obtain the surface and contour plots for the function f unction z=x^2-2xy+4y^2, showing the minimum at x=y=0.
Chapter.5 1-find x,y, and z in the following equations: 6x-3y+4z=41 12x+5y-7z=-26 -5x+2y+6z=14 2-use matlab to solve the following equations for x, y, and z as a functions of the parameter c, and plot the solutions for x, y, and z versus c on the same plot for: -10<=c<=10. x-5y-2z=11c 6x+3y+z=13c 7x+3y-5z=10c 3-use matlab to find the coefficients of the quadratic polynomial y=ax^2+bx+c that passes through these three points: (x,y)=(1,4),(4,73),(5,120).
Chapter.6 1-find the results of the following operations: a- z=6>3+8 b- z=4<7*3 c- z=(4<7)*3 2-use matlab to determine how many times that A larger than B: A=[19 18 22 21 25 19 17 21 27 29] B=[22 17 20 19 24 18 16 25 28 27] 3- if w=[2 4 8]; x=[2 1 5]; y=[0 3 7]; and z=[4 9 1], use matlab to find: a- (x= =y)&((y= =y)&((y= =z)|(x= =z)|(x= =z)) b- (xz)| >z)|((x< ((xw) x>w)) )
4-rewrite the following statement to use only one if statement: if x
5-electrical resistors are said to be connected “in series” if the same current passes through each and “in parallel” if the same voltage is applied across each. So: - in seri series es:: Req= Req=R1 R1+R +R2+ 2+R3 R3+…… +………. …..+ .+Rn Rn.. - In paralle parallel: l: Seq=S1 Seq=S1+S2 +S2+S3 +S3+…… +……….+ ….+Sn, Sn, where where S=1/R. S=1/R. Write M-file that prompts the user for the type of connection and the number of resistors n and then computes the equivalent resistance. 37
Chapter.7 1-the following list gives the measured gas mileage in miles per gallon for 22cars of the same model. Plot the absolute and ralative frequency histogram. 23 25 26 25 27 25 24 22 23 25 26 26 24 24 22 25 26 24 24 24 27 23 2-for the above data, plot the scaled frequency, compute the mean and standard deviation and use them to estimate the lower and upper limits of gas mileage corresponding to 68percent of cars of this model. 3-the following table of temprature versus time data is missing readings at 5 and 9 hours. Use linear interpolation with matlab to estimate the temprature at those times. Time ime(hrs (hrs)) Temp Temp(º (ºC) C)
1 2 3 4 5 10 12 18 24 ?
6 7 8 9 21 20 18 ?
10 10 15
11 11 13
12 12 8
Chapter.8 1-an object moves at a velocity v(t)=5+7t^2 starting from the position x(2)=5 at t=2. Determine its position at t=10. 2-two electrons a distance x apart repel each other with a force k/x^2, where k is a constant. Let one electron be fixed at x=0. Determine the work done by the force of repulsion in moving the second charge from x=1 to x=5. 3-a certain object moves with the velocity v(t) given in the following table. Determine the object’s position x(t) at t=10 seconds if x(0)=3. Time(sec) Velo Veloci city ty(m (m/s /s))
0 0
1 2
2 5
3 7
4 5 6 7 8 9 10 9 12 12 15 18 22 20 17
Chapter.9 1-two polynomials in the variable x are represented by the coefficient vector p1=[6 27 -3] and p2=[10 –5 8]. Use matlab to find the product of these two polynomials, and find the numeric value of the product if x=2. 2-the low of cosines for a triangle states that a^2=b^2+c^2-2bc cos A, where a is the length of the side opposite the angle A, and b and c are the lengths of the other sides. Use matlab to solve for b. suppose that A=60º, a=5m, c=2m, determine b. 3-find the taylor series for exp(x)sin x about x=0 in two ways: a- by multipl multiplying ying the the taylor taylor series for exp(x) exp(x) and and that for sin x. b- by using using the the taylor taylor funct function ion dire directly ctly.. 4-use matlab to compute the following limits as x goes to 0 and 5, respectively: a- (x^4 (x^4+2 +2x^ x^2) 2)/(x /(x^3 ^3+x +x)) b- (x^2-2 (x^2-25)/ 5)/(x^ (x^2-1 2-10x+ 0x+25) 25) 5-for the following equation: 3y’’+18y’+102y=f(t); where f(t)=0 for t<0 and f(t)=10 for t>=0. a- use matlab matlab to compute compute and and plot plot y(t) when y(0)=y’(0)=0. y(0)=y’(0)=0. b- use matlab matlab to compute compute and plot y(t) y(t) when when y(0)=0 y(0)=0 and y’(0)=10. y’(0)=10.
38
مت بحمد ّ لها ENG.A.ARAR
39