MATLAB Examples - Numerical Differentiation

2y ago
2 Views
2 Downloads
1,018.97 KB
18 Pages
Last View : 1m ago
Last Download : 3m ago
Upload by : Philip Renner
Transcription

MATLAB ExamplesNumerical DifferentiationHans-Petter Halvorsen

Numerical DifferentiationThe derivative of a function 𝑦 𝑓(π‘₯)is a measure of how 𝑦 changes withπ‘₯.A numerical approach to the derivative of a function 𝑦 𝑓(π‘₯) is:Note! We will use MATLAB in order to find the numeric solution – not the analytic solution

Numerical DifferentiationMATLAB Functions for Numerical Differentiation:diff()polyder()MATLAB is a numerical language and do not perform symbolicmathematics. well, that is not entirely true because there is β€œSymbolicToolbox” available for MATLAB.

Numerical DifferentiationGiven the following equation:𝑦 π‘₯ ' 2π‘₯ * π‘₯ 3 Find-.-/analytically (use β€œpen and paper”). Define a vector x from -5 to 5 and use the diff() function to .approximate the derivative y with respect to x ( ). / Compare the data in a 2D array and/or plot both the exact value-.of and the approximation in the same plot.-/ Increase number of data point to see if there are any difference.

Given the following equation:𝑦 π‘₯ ' 2π‘₯ * π‘₯ 3Then we can get the analytically solution:𝑑𝑦 3π‘₯ * 4π‘₯ 1𝑑π‘₯

Symbolic Math ToolboxWe start by finding the derivate of f(x) using the Symbolic Math Toolbox:clearclcsyms f(x)syms xf(x) x 3 2*x 2 -x 3This gives:dfdt(x) 3*x 2 4*x - 1dfdt diff(f, x, arted-with-symbolic-math-toolbox.html

x -5:1:5;% Define the function y(x)y x. 3 2*x. 2 - x 3;% Plot the function y(x)plot(x,y)title('y')% Find nummerical solution to dy/dxdydx num diff(y)./diff(x);dydx exact 3*x. 2 4.*x -1;dydx [[dydx num, NaN]', dydx exact']% Plot nummerical vs analytical solution to dy/dxfigure(2)plot(x,[dydx num, NaN], x, dydx exact)title('dy/dx')legend('numerical solution', 'analytical solution')Numerical SolutionExact Solution

𝑦 π‘₯ ' 2π‘₯ * π‘₯ 3𝑑𝑦 3π‘₯ * 4π‘₯ 1𝑑π‘₯x -5:1:5;

𝑑𝑦 3π‘₯ * 4π‘₯ 1𝑑π‘₯x -5:0.1:5;x -5:0.01:5;

Differentiation on PolynomialsGiven the following equation:𝑦 π‘₯ ' 2π‘₯ * π‘₯ 3Which is also a polynomial. A polynomial can be written on thefollowing general form: 𝑦 π‘₯ π‘Ž5 π‘₯ 6 π‘Ž7 π‘₯ 687 π‘Ž687 π‘₯ π‘Ž6 We will use Differentiation on the Polynomial to find-.-/From previous we know that the Analytically solution is:𝑑𝑦 3π‘₯ * 4π‘₯ 1𝑑π‘₯

p [1 2 -1 3];𝑦 π‘₯ ' 2π‘₯ * π‘₯ 3polyder(p)ans 34-1𝑑𝑦 3π‘₯ * 4π‘₯ 1𝑑π‘₯We see we get the correct answer

Differentiation on PolynomialsFind the derivative for the product:3π‘₯ * 6π‘₯ 9 π‘₯ * 2π‘₯We will use the polyder(a,b) function.Another approach is to use define is to first use the conv(a,b)function to find the total polynomial, and then use polyder(p)function.Try both methods, to see if you get the same answer.

% Define the polynomialsp1 [3 6 9];p2 [1 2 0]; %Note!% Method 1polyder(p1,p2)ans 124218p 3% Method 2p conv(p1,p2)polyder(p)36ans 1212362142As expected, the result are the same for the 2 methods used above.For more details, see next page.18180

We have thatand𝑝1 3π‘₯ 2 6π‘₯ 9𝑝2 π‘₯ 2 2π‘₯The total polynomial becomes then:𝑝 𝑝1 𝑝2 3π‘₯ 4 12π‘₯ 3 21π‘₯ 2 18π‘₯As expected, the results are the same for the 2 methods used above:𝑑𝑝 𝑑(3π‘₯ 4 12π‘₯ 3 21π‘₯ 2 18π‘₯) 12π‘₯ 3 36π‘₯ 2 42π‘₯ 18𝑑π‘₯𝑑π‘₯

Hans-Petter Halvorsen, M.Sc.University College of Southeast Norwaywww.usn.noE-mail: hans.p.halvorsen@hit.noBlog: http://home.hit.no/ hansha/

Numerical Differentiation A numerical approach to the derivative of a function ! #(%)is: Note! We will use MATLAB in order to find the numericsolution –not the analytic solution The derivative

Related Documents:

MATLAB tutorial . School of Engineering . Brown University . To prepare for HW1, do sections 1-11.6 – you can do the rest later as needed . 1. What is MATLAB 2. Starting MATLAB 3. Basic MATLAB windows 4. Using the MATLAB command window 5. MATLAB help 6. MATLAB β€˜Live Scripts’ (for algebra, plotting, calculus, and solving differential .

19 MATLAB Excel Add-in Hadoop MATLAB Compiler Standalone Application MATLAB deployment targets MATLAB Compiler enables sharing MATLAB programs without integration programming MATLAB Compiler SDK provides implementation and platform flexibility for software developers MATLAB Production Server provides the most efficient development path for secure and scalable web and enterprise applications

MATLAB tutorial . School of Engineering . Brown University . To prepare for HW1, do sections 1-11.6 – you can do the rest later as needed . 1. What is MATLAB 2. Starting MATLAB 3. Basic MATLAB windows 4. Using the MATLAB command window 5. MATLAB help 6. MATLAB β€˜Live Scripts’ (for

3. MATLAB script files 4. MATLAB arrays 5. MATLAB two‐dimensional and three‐dimensional plots 6. MATLAB used‐defined functions I 7. MATLAB relational operators, conditional statements, and selection structures I 8. MATLAB relational operators, conditional statements, and selection structures II 9. MATLAB loops 10. Summary

Automatic Differentiation Introductions Automatic Differentiation What is Automatic Differentiation? Algorithmic, or automatic, differentiation (AD) is concerned with the accurate and efficient evaluation of derivatives for functions defined by computer programs. No truncation errors are incurred, and the resulting numerical derivative

foundation of basic MATLAB applications in engineering problem solving, the book provides opportunities to explore advanced topics in application of MATLAB as a tool. An introduction to MATLAB basics is presented in Chapter 1. Chapter 1 also presents MATLAB commands. MATLAB is considered as the software of choice. MATLAB can be used .

I. Introduction to Programming Using MATLAB Chapter 1: Introduction to MATLAB 1.1 Getting into MATLAB 1.2 The MATLAB Desktop Environment 1.3 Variables and Assignment Statements 1.4 Expressions 1.5 Characters and Encoding 1.6 Vectors and Matrices Chapter 2: Introduction to MATLAB Programming 2.1 Algorithms 2.2 MATLAB Scripts 2.3 Input and Output

Topographical Anatomy A working knowledge of human anatomy is important for you as an EMT. By using the proper medical terms, you will be able to communicate correct information to medical professionals with the least possible confusion. At the same time, you need to be able to communicate with others who may or may not understand medical terms. Balancing these two facets is one of the most .