Mathematical Applications By Matlab 3rd Day Dr. Wael Khedr CSI Dept. Outline of Third Day: Solve of System of Linear Equations Integral (Symbolic & Numeric) Differentiation Calculus ( Root , Limitation , Polynomial) Graphics (Plots: ezmech , ezsurface) Third Day 2 Solve of System of Linear Equations • Let’s use the matrix approach to solve the system of linear equations for example: • The first step is to write the equations in matrix form: • Next, we need to find the inverse of the A matrix: • To find x and y, multiply the inverse of A by C: Third Day 4 Graphical Solution x=linspace(-10, 5, 20); >> y =(14-2*x)/3; >> y2= 28+4*x; >> hold on >> plot(x,y, 'r') >> plot(x,y2, 'b') Third Day 5 MATLAB Solution >> A = [2 3;-4 1]; >> C = [14;28]; >> X = inv(A)*C X = -5 8 Or : >> X = A\C -5 8 Third Day 6 Example (2) Consider these two equations: MATLAB: >> A = [2 3;4 6]; >> C = [12;28]; >> X = inv(A)*C Warning: Matrix is singular to working precision. X = Inf Inf What’s Wrong? Solve with substitution: Second equation in terms of y: Substitute into first equation: What’s Wrong? Solve: No value of x will satisfy this equation Graphical Solution Example – 3 Equations Write these equations in matrix form: Third Day Example – 3 Equations MATLAB solution: >> A = [12 32 -10; 0 2 3; 7 16 5]; >> C = [-30; 11; 42]; >> X = inv(A)*C X = 7.0000 -2.0000 5.0000 >> Third Day Interpretation of Solution The graphical solution shows that the two equations define parallel lines Since parallel lines never intersect, there is no point that satisfies both equations Therefore, there is no solution to these equations Note that MATLAB solution will result in the same error – the inverse of the coefficient matrix does not exist Summary If the inverse of the coefficient matrix exists, then there is a solution, and that solution is unique If the inverse does not exist, then there are two possibilities: The equations are incompatible, and so there are no solutions, or At least two of the equations are redundant, and so there are more unknowns than unique equations. Therefore, there are an infinite number of solutions Third Day Symbolic Calculations in Matlab: You must first give MATLAB a list of the variable and function names that will appear in the symbolic expressions you will be working with. >> syms x y z a; Determine Integral of function : z ydx x e dx 2 x >> syms x >> y = x^2*exp(x) >> z = int(y) z = x^2*exp(x)-2*x*exp(x)+2*exp(x) Third Day 15 Substitute Numerical values If you would like to substitute numerical values into a symbolic function, use the function subs as in the following way. 𝒇 𝒙,𝒚 = 𝒙𝟐 + 𝐬𝐢𝐧 𝒚 إذا أردت التعويض لحساب ناتج الدالة لقيم المتغيرات يمكن حسابها مباشرة >> f = x^2 + sin(y); >> subs( f, { x , y }, {1 , pi/2 }) Ans = 2 >> int(f,x) X^3/3 + x sin(y) >> int(f, y) y*x^2 – cos(y) % x بالنسبةf تكامل الدالة % y بالنسبةf تكامل الدالة % x بالنسبةf تكامل محدود الدالة ( (a-1)*(a^2 + a + 3*sin(y) +1 ) ) / 3 >> int(f,x 1,a) Third Day 16 Numerically evaluate Single integral (1)Trapezoidal : trapz(x,y) 𝝅 Example: 𝐬𝐢𝐧 𝒙 𝟎 dx %Create a domain vector, X. >> X = 0: pi /100 : pi; %Calculate the sine of X and store the result in Y. >>Y = sin(X); %Integrate the function values contained in Y using trapz. >> Q = trapz(X,Y) Q = 1.9998 Third Day 17 (2) Using Integral( ) function: Create the function 𝒇 𝒙 = 𝒆 −𝒙𝟐 + 𝒍𝒐𝒈 𝒙𝟐 . %Evaluate the integral from x=0 to x=Inf. Solution: >> fun = @(x) exp(-x.^2).*log(x).^2; >>q = integral(fun , 0 , Inf) q = 1.9475 Third Day 18 Numerically evaluate Double integral q = integral2(fun, xmin ,xmax , ymin , ymax) Example: 𝑦𝑚𝑎𝑥 1 1 dx dy 0 0 𝑥+𝑦(1+𝑥+𝑦) where: ymax= 1-x Solution: >> fun = @(x,y) 1./( sqrt(x + y) .* (1 + x + y).^2 ) >> ymax = @(x) 1 - x >> q = integral2(fun,0,1,0,ymax) q = 0.2854 Third Day 19 Determine Differentiation of function : >> f = x^2 + sin(y); >> diff(f,x) % x بالنسبةf تفاضل الدالة Ans= 2*x >> diff(f,y) Ans= cos(y) % y بالنسبةf تفاضل الدالة second derivative of f with respect to x >>diff( f, x, 2); Ans= 2 20 Limitation: 𝒍𝒊𝒎 𝒇(𝒙) 𝒙→𝟎 ≫ 𝒍𝒊𝒎𝒊𝒕 𝒇, 𝒙, 𝟎 Ans= sin(y) We can solve the system of equations x + y = 0 and x + 2y = −1 using the following command >> f1 = x + y; >> f2 = x + 2*y + 1; >> xy_solution = solve(f1,f2,’x,y’); >> x_solution = xy_solution.x 1 >> y_solution = xy_solution.y; -1 Third Day 21 Finding roots of any function:. >> syms x >> ff =2*x^2 + 4*x - 8; >> solve(ff , x) ans = 5^(1/2)-1 -1-5^(1/2) >> double(ans) -3.2361 1.2361 Consider the following Polynomial >> ff =[2 5 -8]; >>roots( f ) Third Day 22 Thank You… 23 To demonstrate these plot types create a symbolic version of “peaks” We broke this function up into three parts to make it easier to enter into the computer. Notice that there are no “dot” operators used in these expressions, since they are all symbolic. When we created the same plots using a standard MATLAB approach it was necessary to define an array of both x and y values, mesh them together, and calculate the values of z based on the two dimensional arrays. The symbolic plotting capability contained in the symbolic toolbox makes creating these graphs much easier. All of these graphs can be annotated using the standard MATLAB functions such as title, xlabel, text, etc. These contour plots are a two-dimensional representation of the threedimensional peaks function The polar graph requires us to define a new function Any of these ezplot graphs can handle parameterized equations