Download presentation
Presentation is loading. Please wait.
Published byMarilynn Thornton Modified over 6 years ago
1
Introduction to Programming for Mechanical Engineers
Lecture 9 (1) MATLAB an Introduction with Applications, Amos Gilat, 3rd ed, Wiley.Mohammad Y. Saadeh
2
Quote of the day “Education is what remains after one has forgotten what one has learned in school.” - Albert Einstein
3
Things you should have known so far!
In chapter 8, we discussed the polynomials and curve fitting. The evaluation of polynomials at certain points is very easy application in MATLAB. Polynomials addition, subtraction, multiplication and division is performed when the polynomials are defined as vectors. Interpolation was also presented as being a useful application of polynomials. Curve fitting can also be performed in the plot window. (1) MATLAB an Introduction with Applications, Amos Gilat, 3rd ed, Wiley.Mohammad Y. Saadeh
4
Numerical analysis: solving equations with one variable
If there is an equation which can be written as f(x)=0, then the solution for this equation is easily obtained. x=fzero(function,x0) is the command used to find the solution of a function. You can define the function is several ways: Function p Evaluation Define it as a string >> w = fzero('x^3-sin(x)',2) w = 0.9286 Define it as an anonymous function, then use its name >> fun fun = @(x)x^3-sin(x) >> v = fzero(fun,2) v = (1) MATLAB an Introduction with Applications, Amos Gilat, 3rd ed, Wiley.Mohammad Y. Saadeh
5
Numerical analysis: plotting equations with one variable
Using fplot command, one can plot the function in the interval provided within the command line. fplot('x^3-sin(x)',[0 4]) will plot the provided function within the [0 4] interval. >> fplot('x^3-sin(x)',[0 4]) (1) MATLAB an Introduction with Applications, Amos Gilat, 3rd ed, Wiley.Mohammad Y. Saadeh
6
Numerical analysis: minimum and maximum of a function
fminbnd is the command used to find the minimum of a function. The syntax is fminbnd(function,x1,x2). When the command executes, it searches for a local minimum and compare it to the ends of the interval. To find a maximum, you need to multiply the function by -1 and find its minimum. >> r = fminbnd('x^3-sin(x)',0,4) r = 0.5354 (1) MATLAB an Introduction with Applications, Amos Gilat, 3rd ed, Wiley.Mohammad Y. Saadeh
7
Numerical analysis: numerical integration (quad & quadl)
Functions with one variable can also be integrated with respect to that variable over a certain period. p = quad(function,a,b) is the command to be used to find the numerical integration in the interval [a b]. The function inserted here should carefully written to represent element by element operations. >> y = quad('x.^3-sin(x)',0,4) y = >> y = quadl('x.^3-sin(x)',0,4) (1) MATLAB an Introduction with Applications, Amos Gilat, 3rd ed, Wiley.Mohammad Y. Saadeh
8
Numerical analysis: numerical integration (trapz)
p = trapz(x,y) is the command to be used to find the integration of given data points (x-y pairs). It uses trapezoidal method to integrate these points. Take the same function, define x = linspace(0,4,5); y = x.^3-sin(x); plot(x,y); p = trapz(x,y) >> x = linspace(0,4,5); >> y = x.^3-sin(x); >> plot(x,y,'r') >> p = trapz(x,y) p = (1) MATLAB an Introduction with Applications, Amos Gilat, 3rd ed, Wiley.Mohammad Y. Saadeh
9
Numerical analysis: ordinary differential equation (ODE)
Ordinary differential equations are the ones of the form for Steps: Write the differential equation in the standard form. Create a user defined or an anonymous function. Define the interval [t0 tf] and the initial condition y0. Choose a solver and perform integration. (1) MATLAB an Introduction with Applications, Amos Gilat, 3rd ed, Wiley.Mohammad Y. Saadeh
10
Numerical analysis: ordinary differential equation (ODE)
solver Description Method ode45 For non-stiff problems, one step solver. Best as an initial method1. Please refer to the above URL for more reading about these solvers. ode23 For non-stiff problems, one step solver. Quicker but less accurate than ode451 ode113 For non-stiff problems, multistep solver1. ode15s For stiff problems, multistep solver. Uses a variable order method. Used if ode45 failed1. ode23s For stiff problems, one step solver. Can solve some problems that ode15 cannot1. ode23t For moderately stiff problems1. ode23tb For stiff problems. More efficient that ode15s1. 1 MATLAB an Introduction with Applications, Amos Gilat, 3rd ed, Wiley. (1) MATLAB an Introduction with Applications, Amos Gilat, 3rd ed, Wiley.Mohammad Y. Saadeh
11
Numerical analysis: ordinary differential equation (ODE)
Let us take an example to illustrate these ideas: % Define the function dx/dt = x^0.2-sin(t) >> fun fun = @(t,x)x^0.2-sin(t) % Use ode45 as it should be an easy solver to begin with >> ode45(fun,[0 3],4) >> [t x] = ode45(fun,[ ],4) t = 1 2 3 x = 4.0000 4.8917 5.3209 6.1619 (1) MATLAB an Introduction with Applications, Amos Gilat, 3rd ed, Wiley.Mohammad Y. Saadeh
12
Numerical analysis: ordinary differential equation (ODE)
Example: % Define the function dx/dt = x^0.2*cos(t)-sin(x*t) >> fun fun = @(t,x)x^0.2*cos(t)-sin(x*t) >> ode45(fun,[-1 6],2) % Use ode45 as it should be an easy solver to begin with >> [t x] = ode45(fun,[0:1:4],2) t = 1 2 3 4 x = 2.0000 2.2678 2.6030 2.0019 1.4543 (1) MATLAB an Introduction with Applications, Amos Gilat, 3rd ed, Wiley.Mohammad Y. Saadeh
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.