0% found this document useful (0 votes)
14 views15 pages

Lec 4

Le

Uploaded by

mo01555761834
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views15 pages

Lec 4

Le

Uploaded by

mo01555761834
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Programming for Mechatronics

“MATLAB”

D r. K h a l e d H a m e d
Lecturer of Mechatronics Engineering

2024
1
Outline

(1) Linear Algebra


(2) Polynomials
(3) Differentiation/Integration

2
Systems of Linear Equations

• Given a system of linear equations


➢ x+2y-3z=5
➢ -3x-y+z=-8
➢ x-y+z=0
• Construct matrices so the system is described by Ax=b
» A=[1 2 -3;-3 -1 1;1 -1 1];
» b=[5;-8;0];

• And solve with a single line of code!


» x=A\b;
➢ x is a 3x1 vector containing the values of x, y, and z

• The \ will work with square or rectangular systems.


• Gives least squares solution for rectangular systems. Solution
depends on whether the system is over or underdetermined.

3
Worked Example: Linear Algebra

• Solve the following systems of equations:

➢ System 1: » A=[1 4;-3 1];


» b=[34;2];
» rank(A)
» x=inv(A)*b;
» x=A\b;

➢ System 2: » A=[2 -2;-1 1;3 4];


» b=[4;3;2];
» rank(A)
➢ rectangular matrix
» x=A\b;
4
More Linear Algebra

• Given a matrix
» mat=[1 2 -3;-3 -1 1;1 -1 1];
• Calculate the rank of a matrix
» r=rank(mat);
➢ the number of linearly independent rows or columns
• Calculate the determinant
» d=det(mat);
➢ mat must be square; matrix invertible if det nonzero
• Get the matrix inverse
» E=inv(mat);
➢ if an equation is of the form A*x=b with A a square matrix,
x=A\b is (mostly) the same as x=inv(A)*b

5
Outline

(1) Linear Algebra


(2) Polynomials
(3) Differentiation/Integration
(4) Differential Equations

6
Polynomials

• Many functions can be well described by a high-order


polynomial

• MATLAB represents a polynomials by a vector of coefficients


➢ if vector P describes a polynomial
ax3+bx2+cx+d

P(1) P(2) P(3) P(4)

• P=[1 0 -2] represents the polynomial x2-2

• P=[2 0 0 0] represents the polynomial 2x3

7
Polynomial Operations

• P is a vector of length N+1 describing an N-th order polynomial


• To get the roots of a polynomial
» r=roots(P)
➢ r is a vector of length N

• Can also get the polynomial from the roots


» P=poly(r)
➢ r is a vector length N

• To evaluate a polynomial at a point


» y0=polyval(P,x0)
➢ x0 is a single value; y0 is a single value

• To evaluate a polynomial at many points


» y=polyval(P,x)
➢ x is a vector; y is a vector of8 the same size
Polynomial Fitting

• MATLAB makes it very easy to fit polynomials to data

• Given data vectors X=[-1 0 2] and Y=[0 -1 3]


» p2=polyfit(X,Y,2);
➢ finds the best (least-squares sense) second-order
polynomial that fits the points (-1,0),(0,-1), and (2,3)
➢ see help polyfit for more information
» plot(X,Y,’o’, ‘MarkerSize’, 10);
» hold on;
» x = -3:.01:3;
» plot(x,polyval(p2,x), ‘r--’);

9
Exercise: Polynomial Fitting

• Evaluate for x=-4:0.1:4.

• Add random noise to these samples. Use randn. Plot the


noisy signal with . markers

• Fit a 2nd degree polynomial to the noisy data

• Plot the fitted polynomial on the same plot, using the same
x values and a red line

10
Exercise: Fitting Polynomials

• By Matlab Find the best second-order polynomial that fits


the points: (-1,0), (0,-1), (2,3).

11
Nonlinear Root Finding

• Many real-world problems require us to solve f(x)=0


• Can use fzero to calculate roots for any arbitrary function

• fzero needs a function passed to it.


• We will see this more and more as we delve into solving
equations.

• For the given function:


y=x3 - 3x2 + 5x+2

» y=@(x) x^3 - 3*x^2 + 5*x+2


» fplot(@(x) x^3 - 3*x^2 + 5*x+2)
» x=fzero(y,[0 -1])
Outline

(1) Linear Algebra


(2) Polynomials
(3) Differentiation/Integration
(4) Differential Equations

13
Numerical Differentiation

• MATLAB can 'differentiate' numerically


» x=0:0.01:2*pi
2*pi;
» y=sin(x);
» dydx=diff(y)./diff(x);
➢ diff computes the first differenc
fferencee

➢ Matlab can compute the differentials by symbols

>> syms x
>> y=sin(x)
>> dydx=diff(y)

14
Numerical Integration

• General integration by MATLAB:

>> syms x

>> fun = x^2 +3*x*log(x)


>> q = int(fun)

OR

q = int( fun,0,15)

Lower limit of integration Upper limit of integration

15

You might also like