Download presentation
Presentation is loading. Please wait.
Published byMyrtle Hardy Modified over 8 years ago
1
Mathematical Applications By Matlab 3 rd Day Dr. Wael Khedr CSI Dept.
2
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
3
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:
4
Third Day 4 Next, we need to find the inverse of the A matrix: To find x and y, multiply the inverse of A by C:
5
Third Day 5 x=linspace(-10, 5, 20); >> y =(14-2*x)/3; >> y2= 28+4*x; >> hold on >> plot(x,y, 'r') >> plot(x,y2, 'b') Graphical Solution
6
Third Day 6 >> A = [2 3;-4 1]; >> C = [14;28]; >> X = inv(A)*C X = -5 8 Or : Or : >> X = A\C -5 8 MATLAB Solution
7
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
8
What’s Wrong? Solve with substitution: Second equation in terms of y: Substitute into first equation:
9
What’s Wrong? Solve: No value of x will satisfy this equation
10
Graphical Solution
11
Example – 3 Equations Write these equations in matrix form: Third Day
12
Example – 3 Equations MATLAB solution: Third Day >> 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 >>
13
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
14
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
15
Symbolic Calculations in Matlab: Third Day 15 give 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 : >> syms x >> y = x^2*exp(x) >> z = int(y) z = x^2*exp(x)-2*x*exp(x)+2*exp(x)
16
Substitute Numerical values Third Day 16 >> subs( f, { x, y }, {1, pi/2 }) Ans = 2 >> int(f,x) % x بالنسبة fتكامل الدالة X^3/3 + x sin(y) >> int(f, y) % y بالنسبة fتكامل الدالة y*x^2 – cos(y) >> int(f,x 1,a) % x بالنسبة fتكامل محدود الدالة ( (a-1)*(a^2 + a + 3*sin(y) +1 ) ) / 3 >> f = x^2 + sin(y);
17
trapz(x,y) Numerically evaluate Single integral (1)Trapezoidal : trapz(x,y) %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
18
Third Day 18
19
Third Day 19 Numerically evaluate Double integral q = integral2(fun, xmin,xmax, ymin, ymax) ExampleExample: where: ymax= 1-x Solution: >> fun = @(x,y) 1./( sqrt(x + y).* (1 + x + y).^2 ) >> ymax = @(x) 1 - x integral2 >> q = integral2(fun,0,1,0,ymax) q = 0.2854
20
20 >> f = x^2 + sin(y); >> diff(f,x) % x بالنسبة fتفاضل الدالة Ans= 2*x >> diff(f,y) % y بالنسبة fتفاضل الدالة Ans= cos(y) Determine Differentiation of function : second derivative of f with respect to x >> diff( f, x, 2 ); Ans= 2
21
Limitation: Third Day 21 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;
22
>> 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 >> ff =[2 5 -8]; >>roots( f ) Third Day 22 Finding roots of any function:. Consider the following Polynomial
23
Thank You… 23
24
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.
25
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.
27
These contour plots are a two-dimensional representation of the three- dimensional peaks function The polar graph requires us to define a new function Any of these ezplot graphs can handle parameterized equations
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.