matlab examples - numerical integration · the integral from 0 to 1 is: % #+&# = 1 3 1 / ≈...

17
MATLAB Examples Hans-Petter Halvorsen Numerical Integration

Upload: others

Post on 11-Aug-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: MATLAB Examples - Numerical Integration · The integral from 0 to 1 is: % #+&# = 1 3 1 / ≈ 0.3333. Numerical Integration An integral can be seen as the area under a curve. Given

MATLABExamples

Hans-PetterHalvorsen

NumericalIntegration

Page 2: MATLAB Examples - Numerical Integration · The integral from 0 to 1 is: % #+&# = 1 3 1 / ≈ 0.3333. Numerical Integration An integral can be seen as the area under a curve. Given

Integration

Theintegralofafunction𝑓(𝑥) isdenotedas:

% 𝑓 𝑥 𝑑𝑥'

(

Page 3: MATLAB Examples - Numerical Integration · The integral from 0 to 1 is: % #+&# = 1 3 1 / ≈ 0.3333. Numerical Integration An integral can be seen as the area under a curve. Given

IntegrationGiventhefunction:

𝑦 = 𝑥+

Weknowthattheexactsolutionis:

% 𝑥+𝑑𝑥 =𝑎-

3

(

/Theintegralfrom0to1is:

% 𝑥+𝑑𝑥 =13

1

/≈ 0.3333

Page 4: MATLAB Examples - Numerical Integration · The integral from 0 to 1 is: % #+&# = 1 3 1 / ≈ 0.3333. Numerical Integration An integral can be seen as the area under a curve. Given

NumericalIntegration

Anintegralcanbeseenastheareaunderacurve.Given𝑦 = 𝑓(𝑥) theapproximationoftheArea(𝐴)underthecurvecanbefounddividingtheareaupintorectanglesandthensummingthecontributionfromalltherectangles(trapezoidrule):

Page 5: MATLAB Examples - Numerical Integration · The integral from 0 to 1 is: % #+&# = 1 3 1 / ≈ 0.3333. Numerical Integration An integral can be seen as the area under a curve. Given

NumericalIntegrationExample:Weknowthattheexactsolutionis:

x=0:0.1:1;y=x.^2;plot(x,y)

% Calculate the Integral:avg_y=y(1:length(x)-1)+diff(y)/2;A=sum(diff(x).*avg_y)

WeuseMATLAB(trapezoidrule):

A = 0.3350Students:Trythisexample

Page 6: MATLAB Examples - Numerical Integration · The integral from 0 to 1 is: % #+&# = 1 3 1 / ≈ 0.3333. Numerical Integration An integral can be seen as the area under a curve. Given

NumericalIntegrationExample:Weknowthattheexactsolutionis:

clearclcclose all

x=0:0.1:1;y=x.^2;

plot(x,y)

% Calculate the Integral (Trapezoid method):avg_y = y(1:length(x)-1) + diff(y)/2;A = sum(diff(x).*avg_y)

% Calculate the Integral (Simpson method):A = quad('x.^2', 0,1)

% Calculate the Integral (Lobatto method):A = quadl('x.^2', 0,1)

InMATLABwehaveseveralbuilt-infunctionswecanusefornumericalintegration:

Page 7: MATLAB Examples - Numerical Integration · The integral from 0 to 1 is: % #+&# = 1 3 1 / ≈ 0.3333. Numerical Integration An integral can be seen as the area under a curve. Given
Page 8: MATLAB Examples - Numerical Integration · The integral from 0 to 1 is: % #+&# = 1 3 1 / ≈ 0.3333. Numerical Integration An integral can be seen as the area under a curve. Given

NumericalIntegration

Giventhefollowingequation:

𝑦 = 𝑥- + 2𝑥+ − 𝑥 + 3

• Wewillfindtheintegralofywithrespecttox,evaluatedfrom-1to1

• Wewillusethebuilt-inMATLABfunctionsdiff(),quad() andquadl()

Page 9: MATLAB Examples - Numerical Integration · The integral from 0 to 1 is: % #+&# = 1 3 1 / ≈ 0.3333. Numerical Integration An integral can be seen as the area under a curve. Given

NumericalIntegration– ExactSolution

Theexactsolutionis:

𝐼 = % (𝑥- + 2𝑥+ − 𝑥 + 3)𝑑𝑥'

(=

𝑥:

4 +2𝑥-

3 −𝑥+

2 + 3𝑥 <(

'

=14 𝑏: − 𝑎: +

23 𝑏- − 𝑎- −

12 𝑏+ − 𝑎+ + 3(𝑏 − 𝑎)

𝑎 = −1and𝑏 = 1 gives:

𝐼 =14 1 − 1 +

23 1 + 1 −

12 1 − 1 + 3 1 + 1 =

223

Page 10: MATLAB Examples - Numerical Integration · The integral from 0 to 1 is: % #+&# = 1 3 1 / ≈ 0.3333. Numerical Integration An integral can be seen as the area under a curve. Given

clear, clc

syms f(x)syms x

f(x) = x^3 + 2*x^2 -x +3

I = int(f)

I(x) = x^4/4 + (2*x^3)/3 - x^2/2 + 3*xThisgives:

SymbolicMathToolboxWestartbyfindingtheIntegralusingtheSymbolicMathToolbox:

http://se.mathworks.com/help/symbolic/getting-started-with-symbolic-math-toolbox.html

Page 11: MATLAB Examples - Numerical Integration · The integral from 0 to 1 is: % #+&# = 1 3 1 / ≈ 0.3333. Numerical Integration An integral can be seen as the area under a curve. Given

clear, clcsyms f(x)syms xf(x) = x^3 + 2*x^2 -x +3

a = -1;b = 1;Iab = int(f, a, b)

Iab = 22/3Thisgives:

SymbolicMathToolboxTheIntegralfromatob:

http://se.mathworks.com/help/symbolic/getting-started-with-symbolic-math-toolbox.html

≈ 7.33

Page 12: MATLAB Examples - Numerical Integration · The integral from 0 to 1 is: % #+&# = 1 3 1 / ≈ 0.3333. Numerical Integration An integral can be seen as the area under a curve. Given

clear, clc

x = -1:0.1:1;

y = myfunc(x);

plot(x,y)

% Exact Solutiona = -1;b = 1;Iab = 1/4*(b^4-a^4 )+2/3*(b^3-a^3 )-1/2*(b^2-a^2 )+3*(b-a)

% Method 1avg_y = y(1:length(x)-1) + diff(y)/2;A1 = sum(diff(x).*avg_y)

% Method 2A2 = quad(@myfunc, -1,1)

% Method 3A3 = quadl(@myfunc, -1,1)

Iab = 7.3333A1 = 7.3400A2 = 7.3333A3 = 7.3333

MATLABgivesthefollowingresults:

Page 13: MATLAB Examples - Numerical Integration · The integral from 0 to 1 is: % #+&# = 1 3 1 / ≈ 0.3333. Numerical Integration An integral can be seen as the area under a curve. Given
Page 14: MATLAB Examples - Numerical Integration · The integral from 0 to 1 is: % #+&# = 1 3 1 / ≈ 0.3333. Numerical Integration An integral can be seen as the area under a curve. Given

IntegrationonPolynomials

Giventhefollowingequation:

𝑦 = 𝑥- + 2𝑥+ − 𝑥 + 3

Whichisalsoapolynomial.Apolynomialcanbewrittenonthefollowinggeneralform:𝑦 𝑥 = 𝑎/𝑥@ + 𝑎1𝑥@A1 + ⋯+ 𝑎@A1𝑥 +𝑎@

• Wewillfindtheintegralofywithrespecttox,evaluatedfrom-1to1• Wewillusethepolyint() functioninMATLAB

Page 15: MATLAB Examples - Numerical Integration · The integral from 0 to 1 is: % #+&# = 1 3 1 / ≈ 0.3333. Numerical Integration An integral can be seen as the area under a curve. Given

clearclc

p = [1 2 -1 3];

polyint(p)

Thesolutionis:ans =

0.2500 0.6667 -0.5000 3.0000 0

Thesolutionisanewpolynomial:[0.25, 0.67, -0.5, 3, 0]

Whichcanbewrittenlikethis:

0.25𝑥: + 0.67𝑥- − 0.5𝑥+ + 3𝑥

Weknowfromaexamplethattheexactsolutionis:

% (𝑥- + 2𝑥+ − 𝑥 + 3)𝑑𝑥'

(=

𝑥:

4 +2𝑥-

3 −𝑥+

2 + 3𝑥 <(

'

→Soweesetheansweriscorrect(asexpected).

Page 16: MATLAB Examples - Numerical Integration · The integral from 0 to 1 is: % #+&# = 1 3 1 / ≈ 0.3333. Numerical Integration An integral can be seen as the area under a curve. Given
Page 17: MATLAB Examples - Numerical Integration · The integral from 0 to 1 is: % #+&# = 1 3 1 / ≈ 0.3333. Numerical Integration An integral can be seen as the area under a curve. Given

Hans-PetterHalvorsen,M.Sc.

UniversityCollegeofSoutheastNorwaywww.usn.no

E-mail:[email protected]:http://home.hit.no/~hansha/