+ All Categories
Home > Documents > numerical Methods with FreeMat

numerical Methods with FreeMat

Date post: 10-Feb-2018
Category:
Upload: preveenrrt4142
View: 226 times
Download: 0 times
Share this document with a friend

of 40

Transcript
  • 7/22/2019 numerical Methods with FreeMat

    1/40

    Basic Numerical Methods and FreeMat

    By Timothy Cyders and Gary Schaefer

  • 7/22/2019 numerical Methods with FreeMat

    2/40

    Essentially, all models are wrong, but some are useful.

    - George Box

    We demand guaranteed, rigidly-defined areas of doubt and uncertainty.

    - Douglas Adams

  • 7/22/2019 numerical Methods with FreeMat

    3/40

    About This Document

    In a nutshell,

    Students are poor. Commercial softare pac!ages such as "AT#AB are expensi$e. %ree"atis free, as in freedom, and as in &eer.

    Students are poor. 'ngineering("athematics texts are expensi$e. This document is free asin freedom, and as in &eer.

    A lot of &asic ideas put forth in most 'ngineering("athematics texts are rather old, and aregenerally common !noledge, at least to the educated. 'specially ith respect to explanations ofcommercial softare pac!ages such as "AT#AB, and introductions(explanations of numericalmethods, texts tend to under-explain and o$ercharge. "y personal experience ith college textshas &een that the ones here explanation ould ha$e greatly augmented learning lac!ed suchexplanation, &ut certainly didn)t hold &ac! on cost.

    This text is a response to that phenomenon. This is a &asic introduction to numerical methods,shoing ho the commonly used methods or! &y some simple examples. Also, it)s a &it of anintroduction to the language of %ree"at, hich is easily interchangea&le ith commercialpac!ages such as "AT#AB, or is at the least reada&le pseudo-code for examples to use in otherlanguages *such as C++ or %TA/. 0ro&a&ly the most significant aspect of this document isthat it is free *as in freedom/. If there are pictures, asides, explanations or examples that can ma!elearning &etter, you can add them and repu&lish the document *citing the original or!,

    o&$iously/1

    There are certain things that this text is and isn)t. This text is a &asic introduction. It is by no meansa full-on graduate-le$el text on the intricacies of numerics and solution of differential e2uations.This text, as you)ll see, doesn)t present much of anything in the ay of analytical solution ofdifferential e2uations. This text $ery lightly addresses error and uncertainty, &ut su&stantiallyaddresses examples of &asic pro&lems engineers may face. This text isa &asic introduction to theidiosyncrasies of the %ree"at programming en$ironment, and doespro$ide donloada&leexamples of e$ery type of pro&lem co$ered.

    This text is notfor commercial sale, and may not &e printed, copied or modified for the purpose ofselling it. This textis free to copy, distri&ute, donload and disseminate 3 e encourage exactlythis. 4e 5ust as! that you gi$e %ree"at a try, and you might fall in lo$e ith it li!e e ha$e.

  • 7/22/2019 numerical Methods with FreeMat

    4/40

  • 7/22/2019 numerical Methods with FreeMat

    5/40

    Basic Numerical Methods and FreeMat

    umerical methods pro$ide a ay to sol$e pro&lems 2uic!ly and easily compared to analyticsolutions. 4hether the goal is integration or solution of complex differential e2uations, there aremany tools a$aila&le to reduce the solution of hat can &e sometimes 2uite difficult analyticalmath to simple alge&ra and some &asic loop programming. %ree"at has a &ig speed ad$antage interms of simple looping than!s to the ne IT compiler, hich can run loops and other simpleprogramming as fast if not much faster than e$en most commercial pac!ages such as "AT#AB.

    Section 1: Root Finding

    ne of the most &asic applications of numerical methods is to find the roots of a single e2uation.In cases here an e2uation)s solution cannot &e o&tained &y alge&raic means *as is the case ithmany non-linear e2uations/, there are se$eral methods for finding the roots *solutions/ ith thepoer of a computer and some &asic algorithms, hich ill &e discussed here.

    1.1 - The Bisection Method

    If you ha$e e$er searched in a phone &oo! for a name, you)$e intuiti$ely performed something li!ethe &isection method. The &isection method is a simple ay to find a single root of an e2uation,gi$en that the root exists &eteen to &ounds *hich must &e defined at the outset of thescript/, and it is the only root &eteen those &ounds. The method then reduces these &ounds &yhalf, alays selecting the half containing the root, until the &ounds are reduced &elo anaccepta&le error limit.

    'xample9 #et)s examine the nonlinear e2uation

    x2

    4sin x =0

  • 7/22/2019 numerical Methods with FreeMat

    6/40

    4e can 2uic!ly notice se$eral things a&out this e2uation. %irst, it can &e descri&ed as theintersection of to functions,

    f 1x =x

    2

    4

    f 2x =sin x

    Second, e see &oth &y inspection of either graph that there are to solutions, one at ?ero *atri$ial solution/ and one near :. %inally *and most pertinently to the su&5ect under discussion here/,e see that this e2uation cannot be solvedith con$entional alge&ra, so the only ay to o&tain asolution ithout using something li!e a Taylor series expansion is either graphically or &y a

    numerical method. 4e)ll approach this pro&lem using the &isection method. The &asic operatingprinciple of this method is as follos9

    You are looking for the name Stevens in a honebook. !ick u the honebook, and oenit to the middle, you will find names beginning with ". Which half of the book isStevens in# $s Stevens is in the latter half, we then take that half of the book, and slitit in two. %he resulting age has the name &euben on it. Stevens comes after &euben'i.e.( Stevens is in the second half), and so we slit the second half again. *ontinue thisrocess, always selecting the half containing the solution you are looking for, and youwill find the age with Stevens on it in a matter of seconds, even with a largehonebook. You reduce your bounds by a factor of two each iteration, so the method

    converges very +uickly

    4e can no apply this method to root finding using a hile loop ith some simple &ooleanoperators *if-then or true-false statements/. %irst, e ill determine hich half of our &oundscontains the solution. Then, e)ll reduce our &ounds &y setting a ne point for either the upperlimit or loer limit to the midpoint of the &ounds, and reiterate the selection process. After 5ust afe iterations, e ill narro our &ounds 2uic!ly to the point that e can estimate an anser.#et)s start ith a set of &ounds that surround the root in 2uestion *and onlythe root in 2uestion1/,

  • 7/22/2019 numerical Methods with FreeMat

    7/40

    E6 7F. An example code ould loo! li!e so9

    % bisection.m

    %% --- Define Bounds and Error --- %%hi = 3; % Upper boundlow = 1; % Lower bound

    epsilon = e-!; % "cceptable error for solutioncounter = #; % $ounter to a&oid infinite loops'limit = 1###; % Limit number of iterations to a&oid infinite loops

    %% --- (tart loop --- %%while hi-low' ) epsilon ** counter + limit; % $,cle bound reductionuntil solution or maits reached

    mid = hi low' / ; % (et midpoint halfwa, between lo and hi

    if hi0hi/' - sinhi''0mid0mid/' - sinmid'' + #' %e&aluate % 2' at mid and hi and multipl, the results 4 a ne5ati&eresult means % the function crosses an ais a root6'

    low = mid; % Eliminate lower half if it doesn7t contain the

    %root elsehi = mid; % Eliminate upper half if it doesn7t contain the

    %rootend

    counter = counter 1; % 8ncrease counter b, 1 for each iterationend

    mid = hi low' / % (et midpoint halfwa, between lo and hi one last% time to output solution'

    'xecute this script, and it ill arri$e at the anser 6.778. If e plug this into the originale2uation, e get a $ery small error on the order of 6=8,

  • 7/22/2019 numerical Methods with FreeMat

    8/40

    *so e compare the product to

  • 7/22/2019 numerical Methods with FreeMat

    9/40

    p i1 = p i f pi

    f 'pi

    here i is counting iterations, starting at 6. 'ssentially, the ne guess *p i+6/ is displaced along the x-axis from the old guess *p i/ &y the function $alue o$er the slope9

    4e continue this process until each ne iteration of p increases &y an inter$al less than ouraccepta&le error *i.e. Lpi+6 - piL M error/. o, to do this in %ree"at, e)ll use a hile loop again,&ecause e)re not sure 5ust ho many iterations e ill need. 4e)ll also use a method calledanonymous functions that allos you to pass a function as a $aria&le. This ill help condense ourcode, and allo for future modification to our program. %irst, e)ll define our function f, and itsderi$ati$e fp9

    f = 9' .:/' 4 sin'';fp = 9' / 4 cos'';

    otice the decimal after x in the first relation 3 this ensures that the matrix elements are s2uared,instead of %ree"at trying to s2uare the matrix, hich is a different operation all together re2uiringthe matrix to ha$e specific properties. o, at the command indo, if you type

    --) f#'ans = #--) f1'ans = -.#!1!

    4e can see from the plot of our function on page ;> that these $alues are simply the e$aluation ofthe function at those to points. So no, e can set our error and initial guess, and form a hilestatement to do our looping for us9

    dx

    dy

    f(p) =dy

    f '(p) =dy

    dx

    dx= dy

    (dydx)=

    f(p)f '(p)

  • 7/22/2019 numerical Methods with FreeMat

    10/40

    error = e-!; % (et error boundp = 3; % 8nitial 5uessi = 1; % (tart counter at 1pi1' = pi' - fpi''/fppi''; % #1331.!#3?>##!?#1.331!3!#1.33!3??!!>31.33!3>?#

    otice that each iteration, e dou&le the num&er of accurate digits after our first guess. The lastiteration is accurate to all the decimal places shon in long format. If e plug p*>/ into our originalfunction, e should get something veryclose to ''

    ans =

    !.!!111!131!?e-#1>

    ur last guess as actually accurate on the le$el of 6e-6>1 eton)s method is $ery poerful, &utyou ha$e to !no the deri$ati$e &eforehand. Also, if you pass a minimum or maximum in thefunction *or a spot here the deri$ati$e approaches

  • 7/22/2019 numerical Methods with FreeMat

    11/40

    root. As the method con$erges, our guess gets closer and closer to ?ero. Kere are se$eral imagesdepicting hat happens - gi$en again our test function9

    f x =x2

    4sinx

    4e pic! to pointsN in this case e)ll use 7.8 and :.8. o, e)ll dra a line &eteen the topoints and find its root using the folloing formula9

  • 7/22/2019 numerical Methods with FreeMat

    12/40

    xn1 = xn xn xn1

    fxn fxn1 fxn

    The x-intercept of our line turns out to &e :.6=. #et)s chec! to see ho close e are to ?ero9

    2.10642

    4 sin 2.1064 = 0.2493

    4e)re still a fair &it from the root, so no e)ll use the points :.8 and :.6= for our next iteration9

  • 7/22/2019 numerical Methods with FreeMat

    13/40

    Osing our formula again, e get 6.>6. Again, e)ll chec! our anser, and see ho close e areto the root9

    1.9691

    2

    4 sin 1.9691 = 0.0477

    Getting closer1 As e continue this process, e e$entually find our result at 6.77, ourapproximate root. So, ho do e code thisP Kere)s an example script9

    %secantmethod.m script = @3.!; .!A; % arra, of 5uessesn = ;

    while n':'/ 4 sinn'' ) 1e-!'n1' = n' 4 n' - n-1''/n':'/ 4 sinn''' -

    n-1':'/ 4 sinn-1'''''0n':'/ 4 sinn''';n = n1;

    end

    If e run this from the %ree"at command line, e get9

    --) secantmethod--) ans = 3.!############# .!############# .1#>3>1!!3 1.>133!?!3!# 1.3>>?#?> 1.33?#?>3!31! 1.33!3??1?#

  • 7/22/2019 numerical Methods with FreeMat

    14/40

    0erhaps a more ro&ust program ould allo us to simply pass our function and to initial guessesas arguments9

    function ret = secantf1'

    = @1; A; % initial 5uessesn = ; % initiate counter for loop

    whileabsfn'' ' ) 1e-!' % precision of 1e-!n1' = n' - n' - n-1''/fn'' - fn-

    1''''0fn'';n = n1;

    end

    ret = n'; % return last &alue of our root'

    o, e can pass any function e li!e, ith to initial guesses9

    --) f = 9' .:! - 4 1'; % we can7t sol&e this one anal,ticall,--) secantf#.1' % one of the roots is somewhere near 1

    ans = 1.1>3#3??!#

    --) fans' % let7s chec our answer

    ans =->.?>?!1>1!#?e-#

    The secant method con$erges a &it sloer than eton)s "ethod in most cases, &ut tends to &esomehat more sta&le, and is easier to em&ed into adapti$e code that helps it a$oid di$ergence.

  • 7/22/2019 numerical Methods with FreeMat

    15/40

    1.4 - Nonlinear Systems of Equations

    0erhaps a more interesting *and more useful/ application of root-finding is to sol$e systemsof non-linear e2uations. In many areas of science, e ant to model hole systems, hich can in manycases pro$e difficult to sol$e analytically, so e can use the method descri&ed here toapproximate some solutions.

    The Newton-Rahson Method for "#stems of $%uations

    Section 6.7 detailed eton)s "ethod to sol$e for the roots of a nonlinear e2uation. But hat ife ha$e a hole system of e2uationsP eton-aphson also or!s for systems of e2uations, andis relati$ely simple *especially in %ree"at1/ hen e use a matrix approach. I)ll s!ip some theoryhere, &ut &y the same assumptions made in eton)s "ethod, e can arri$e at the folloinge2uation9

    [ f1x1

    f1x2

    f2x1

    f2x2 ][x1x2 ]=[f 1f2]

    The first matrix seen here is called the aco&ian "atrix, simply a matrix ith $alues for partialderi$ati$es corresponding to their location inside the matrix. If you loo! at this e2uation, it has theclassic Ax Q & appearance, hich means e can simply use AR& *Gaussian 'limination/ to sol$e for

    x. Gi$en that the only un!nons in this e2uation are x6and x:, e start ith a guess *5ust as in

    eton)s "ethod/, and use Gaussian 'limination to sol$e for these $alues. 4e then get our ne$alues for x6and x:from our original x6and x: as follos9

    [x1x2]new= [x1x2]old [

    x 1 x 2]

    4e iterate this idea until our delta matrix *the error/ &ecomes smaller than some threshold thate set, and e can then say )close enough1). Straight aay, e can ma!e a simple program toe$aluate this method for a system of to e2uations ith to un!nons. #et)s 5ust put thee2uations and their deri$ati$es right into the code and ma!e a script .m file9

    % newts,s.m script file

    %% --- Cwo (imple Cest 2unctions --- %%f1 = 9,' : 4 0,:3'';f = 9,' sin' 30cos30,'';

    %% --- Cheir Deri&ati&es --- %%df1d = 9,' 0';dfd = 9,' cos'';df1d, = 9,' -10,:';

  • 7/22/2019 numerical Methods with FreeMat

    16/40

    dfd, = 9,' -0sin30,'';

    , = @1;1A; % 8nitial 31.>>#13'

    ans =

    .>>!e-1!

    So, it or!ed1 This techni2ue can &e easily extrapolated to systems of many e2uations and manyun!nons, ith to ca$eats9 all partial deri$ati$es ha$e to &e !non *or numericallyapproximated/, and in such systems, many times there are multiple solutions. This method can

  • 7/22/2019 numerical Methods with FreeMat

    17/40

    di$erge, and the solutions reached are usually hea$ily dependent on your initial guesses, so it)snecessary to ha$e an idea of hat your functions loo! li!e in the first place, and here thesolutions might &e. ou)$e &een arned.

    o, let)s loo! at a real-orld example, along ith a program that allos us some input andmanipulation. inematic analysis of the four &ar mechanism shon &elo results in the folloing

    system of e2uations9

    [f1 3, 4

    f2 3, 4

    ] =

    [r4 cos4 r3cos 3 r2 cos2 r1

    r4sin 4 r3sin 3 r2 sin 2

    ]Kere, all radii *lin!age lengths/ are !non, as is 2, so e really 5ust ha$e to functions, f6and f:ofto $aria&les, 3and 4. %or the aco&ian, e ta!e the partial deri$ati$es as shon pre$iously9

    [ f1 3

    f1 4

    f2 3

    f2 4

    ] = [ r3sin 3 r4 sin4r3cos 3 r4 cos4]o, if e ha$e an initial guess at 3and 4, e can plug them into our iterati$e techni2ue, ande$entually get real, accurate $alues1 #et)s start &y coding our functions and deri$ati$es asanonymous functions *functions e can enter right from the command console, and pass aroundli!e $aria&les/. 4e)ll assume here that you !no the $alues for r6, r:, r7, r= and th:. Instead oftyping their $aria&le names here, type in their actual $alues *e.g.9 f6Q*th7,th=/*6.:Jcos*th=/...etc./

  • 7/22/2019 numerical Methods with FreeMat

    18/40

    f1 = 9th3 th' r0costh' - r30costh3' r0costh' r1;f = 9th3 th' r0sinth' - r30sinth3' r0sinth';df1dth3 = 9th3 th' r30sinth3'';dfdth3 = 9th3 th' -r0sinth'';df1dth = 9th3 th' -r30costh3'';dfdth = 9th3 th' r0costh'';

    ext, e)ll put together a function to hich e can pass our e2uations as matrix &loc!s. 4e)llsimply define matrices for f and li!e so9

    f = 9th3th' @f1th3th'; fth3th'A; = 9th3th' @df1dth3th3th' df1dthth3th';dfdth3th3th'dfdthth3th'A;

    and e)ll design our function so e can pass these &loc!s to it as an argument. Kere)s an example9

    function ret = nrs,sf5uess' % Iewton-Japhson for eGuations in unnowns

    epsilon = #.##1; % acceptable errorcounter = 1;delta = @1;1A

    while delta ) epsilondelta = 5uess1'5uess''Hf5uess1'5uess'';5uess = 5uess delta;counter = counter 1;

    end

    fprintf7(ol&ed in %d iterations7counter'

    ret = 5uess;

    But ait, there)s more1 4hat if e don)t !no the analytical deri$ati$es of our functionsP 0erhapsthey)re &ehind a &lac! &ox, or 5ust really difficult to differentiate *or perhaps e 5ust don)t ha$etime/P As e)ll see in the next section, e can calculate the deri$ati$e &y ta!ing hat)s called afinite difference.Simply stated, a deri$ati$e is the rate of change of a function ith respect to achange in its input, or rise o$er run. So, e could calculate an approximate partial deri$ati$e forour four-&ar function a&o$e li!e so9

    2,guess = 40 , 3,guess = 65

    f1 2

    f12,guess, 3,guess f12,guess , 3,guess

    2

    here is relati$ely small. This means e can use the eton-aphson method for systems*effecti$ely the secant method here/ ithout ha$ing to analytically calculate the deri$ati$es1 4esimply su&stitute our estimation of each deri$ati$e into the aco&ian matrix, and sol$e as usual.4e must &e careful, though. umerical differentiation can &e su&5ect to large inaccuracy for a lot

  • 7/22/2019 numerical Methods with FreeMat

    19/40

    of different reasons *hich ill &e discussed a &it in the next section/, so use caution hen youuse this method1

    ne last thing to mention for multiple root finding9 there is no a &uilt-in function in %ree"atcalled fsol$e*/ that performs a eton-aphson $ery much li!e the scripts shon here, &ut ith alittle more teeth and error chec!ing. %or most pro&lems, this &uilt-in su&routine can gi$e you an

    out-of-the-&ox solution should you choose to use it. Simply type )help fsol$e) *sans 2uotes/ in your%ree"at command indo for an explanation of usage.

  • 7/22/2019 numerical Methods with FreeMat

    20/40

    Section 2: Numerical Differentiation

    There are se$eral different methods that can &e used to calculate a numerical deri$ati$e.

    Section 3: Numerical ntegration

    umerical integration is something that computers excel at. In fact, numerical integrals are the&asis for many of the poerful things computers are capa&le of today. 4hether it)s pro5ecting theposition of your mouse cursor on the screen &ased on an infrared diode)s electrical &eha$ior orcalculating the physics effects in your fa$orite computer game, this &asic functionality is integral*pun intended/ to the usefulness of the machine in front of you. )4hy integrate numericallyP), youmight as!. 4ell, ta!e a 2uic! loo! at hat an integral is doingN it)s the sum of a gi$en function,iterated an infinite *or 5ust a high num&er/ of times o$er a $ery small inter$al. %or example, loo! atthis integral9

    0

    2

    x2dx

    4e can sol$e this analytically 2uite easily using an in$erse poer rule. There is no reason tonumerically integrate this formula., &ecause the closed form is easily o&tained. But hat if youe$er encounter this mess of an e2uation9

    z

    1

    x2exp

    xx 2

    2x2

    dx=1

    igure ( /aussian robability density function '!0) curve with a standarddeviation of .

  • 7/22/2019 numerical Methods with FreeMat

    21/40

    This is the calculation for the area from some point ? to infinity under the cur$e of the Gaussian ornormal pro&a&ility density function *0D%/. Such a cur$e is shon in %igure 6. This is actually a $eryimportant function for a lot of different applications, and as an engineer or mathematician, youill indeed use it, especially its integral. The integral of this cur$e is e2ual to the pro&a&ility that anormally distri&uted $aria&le)s $alue lies &eteen to &ounds, expressed in standard de$iationsfrom the mean at ?ero. The total area under the cur$e, from minus infinity to plus infinity, is e2ual

    to one *the function as designed ith that specific characteristic in mind 3 there is a 6

  • 7/22/2019 numerical Methods with FreeMat

    22/40

    4e)re going to use this e2uation to demonstrate some some of the methods of numericalintegration. !ay, &ac! to the e2uation for the Gaussian, also called the normal, pro&a&ilitydensity function. It is9

    PDF = 1

    x

    2

    exx

    2

    2x2

    here9xQ the standard de$iation of the distri&ution. This is an indication of ho ide or fat thedistri&ution is.

    x Q the a$erage of the distri&ution. This is here the center of the distri&ution is located.Osing %reemat, e can create a plot of this function, shon in %igure =.

    This as created using this small &it of code9

    si5ma=1;=-!K#.#1K!;5aussian=1/si5ma00pi':#.!''0ep-.:.#/0si5ma:'';plot5aussian'

    %rom this, e)ll explain and demonstrate se$eral methods for calculating the area underneath thecur$e.

    2.1 - Rectan&ular Rule

    ne ay to add up the area is ith the a &unch of rectangles. ust place a &unch of e2ual-idthrectangles under the area. 'ach rectangle stretches so that one of the top corners touches thecur$e and the &ottom touches the &ottom, hich in this case is ?ero. This is shon in %igure 8.

    igure 6( /aussian robability density function

  • 7/22/2019 numerical Methods with FreeMat

    23/40

    By adding up the areas of each rectangle, e can calculate the area under the cur$e. If e loo! at

    each rectangle as ha$ing a idth and a height e2ual to the distance from ?ero to here ittouches the graph, e)ll ha$e something as shon in %igure >.

    4e)ll no use this this graph and some %ree"at code to add up the area of all of the rectangles.Ko many rectanglesP Good 2uestion1 The graph goes to infinity. But e don)t ant *or need/ togo that high. So ho high do e need to goP ote that the graph falls close to ?ero hen it gets

    a&o$e 7. So, e)ll go a &it a&o$e that and go to 6

  • 7/22/2019 numerical Methods with FreeMat

    24/40

    % each rectan5le touches the 5raph on % the 0ri5ht0 side. Chis side is at % the point of w which is wh, the % counter 5oes from 1w to 1#w with % a step sie of Must w.

    si5ma=1; % Che standard de&iation of the cur&e.% Che net line calculates the hei5ht of each rectan5le.

    h=1/si5ma00pi':#.!''0ep-.:'/0si5ma:'';arearect=w0h; % $alculate the area of each rectan5le.areasum=cumsumarearect'; % (um up the areas of each rectan5le.totalarea=areasumlen5thareasum''; % Chis is the final area.printf7Che area under the cur&e from %f to %f is%f.Hn7startpointstoppointtotalarea';

    Sa$ing this script as gaussian_area.m, then running it, e get the folloing result.

    --) 5aussianareaChe area under the cur&e from 1.###### to 1#.###### is #.1>.

    The area e calculated is :. But is this correctP Another good 2uestion1 ne ay to findout is to ma!e the idth of each rectangle smaller, rerunning the routine, and comparing theanser to this one. #et)s ma!e the idth !?.

  • 7/22/2019 numerical Methods with FreeMat

    25/40

    The area has gone up to 8;. The difference from the old area is 8; - : Qor less. That means a one in a million difference. The great thing is thate can set up %ree"at to decide hen it has reached that threshold. 4e can !eep ma!ing theidth smaller and smaller until it reaches the point here the difference is only 6 in 6 millionth of

    the calculated area.

    4e)re not going to go that far yet. Instead, e)ll use a less precise le$el of 6

  • 7/22/2019 numerical Methods with FreeMat

    26/40

    last_area=total_area; % /e-set the last area0s calculation beforerunnin'% the ne(t loop.endprintf7Che area under the cur&e from %f to %f is%f.Hn7startpointstoppointtotalarea';printf0The number of steps reuired is %i for a precision of%f.n0+len'th(#+precision #;

    ext 2uestion9 4e)re adding up our rectangles going from 6 to 6.Che number of steps reGuired is 1!1 for a precision of #.#1####.

    The change from 6< to :

  • 7/22/2019 numerical Methods with FreeMat

    27/40

    So, ho do e capture that missed areaP The only ay to do so is to decrease the idth of therectangle. As e ma!e the idth smaller and smaller, the amount of missed area ill alsodecrease. The dra&ac!, for us, is that, as e ma!e the idth smaller, the num&er of steps goesup. That means that it)s going to re2uire more calculations to get to the le$el of precision desired.4e ant a le$el of precision of 6< ->. #et)s set the precision as shon here9

    precision=1e->; % Chis is the desired precision.

    o, let)s run the script again.

    --) 5aussianareaChe area under the cur&e from 1.###### to #.###### is #.1!?>!!.Che number of steps reGuired is #3>?1 for a precision of #.#####1.

    4hoa1 That too! a little hile1 At least, it did on my computer6. 0erhaps yours ran faster. Still, itshould ha$e ta!en more time than hen you ran ith the precision set to 6< -7. And ho long did itta!e on my computerP #ong enough for me to al! to the !itchen, gra& a soda from the fridge,and al! &ac!. But ho long as that, preciselyP #et)s find out. 4e)ll use the %reemat commandsof ticand toc. The command ticstarts a timer runningN tocstops it and outputs the time, inseconds. The code no loo!s li!e this, ith the ne code set in &oldface9

    % (cript to calculate the specific area under a

  • 7/22/2019 numerical Methods with FreeMat

    28/40

    % the point of w which is wh, the % counter 5oes from 1w to 1#w with % a step sie of Must w.

    si5ma=1; % Che standard de&iation of the cur&e.

    % Che net line calculates the hei5ht of each rectan5le.

    h=1/si5ma00pi':#.!''0ep-.:'/0si5ma:'';arearect=w0h; % $alculate the area of each rectan5le.areasum=cumsumarearect'; % (um up the areas of each rectan5le.totalarea=areasumlen5thareasum''; % Chis is the final area.

    % $reate a while loop that runs until the precision criteria is met.

    diffarea=totalarea; % Chis will be the difference between loops used % to tell if the precision le&el has been met.

    lastarea=totalarea; % Chis will be used to hold the area from the % pre&ious loop. 8t will be compared with the % area of the current loop.

    precision=1e->; % Chis is the desired precision.while diffarea)totalarea0precision'';w=w/; % (tart b, di&idin5 the width in half.=startpointwKwKstoppointw; % (et the &alues for as before.h=1/si5ma00pi':#.!''0ep-.:'/0si5ma:''; % $alculate h.arearect=w0h; % $alculate the areas of each rectan5le.areasum=cumsumarearect'; % (um up the areas of each rectan5le.totalarea=areasumlen5thareasum''; % Cotal area under the cur&e.diffarea=abstotalarea-lastarea'; % Che difference from the pre&ious % calculation.

    lastarea=totalarea; % Je-set the last area7s calculation before

    % runnin5 the net loop.end

    printf7Che area under the cur&e from %f to %f is%f.Hn7startpointstoppointtotalarea';printf7Che number of steps reGuired is %i for a precision of%f.Hn7len5th'precision ';total_time=toc;printf0The time reuired for this runnin' was %fseconds.n0+total_time#;

    The result is9

    --) 5aussianareaChe area under the cur&e from 1.###### to #.###### is #.1!?>!!.Che number of steps reGuired is #3>?1 for a precision of #.#####1.Che time reGuired for this runnin5 was !.>3### seconds.

    Is it time to trade in the old computerP Kardly. This &east is good for another 6

  • 7/22/2019 numerical Methods with FreeMat

    29/40

    2.2 - Trae'oid Rule

    2.! - "imson(s Rule

    2.) - *aussian +uadrature

    2., - Monte Carlo nte&ration

    "onte Carlo methods are methods that use random num&ers to do different mathematical tas!s.0ro&a&ly the most common usage is for numerical integration, as e)ll descri&e here. This methodcan or! on anything from simple functions to image analysis. Kere)s ho it or!s9

    Say e ere to lay out the domain E< F onto a dart &oard, and plot cos*x/ on that domain. If ethro a random distri&ution of darts at this &oard, it might loo! li!e this9 *red crosses are darts/

    umerically, the proportion of darts Hunder the cur$e *&eteen the cur$e and the x-axis/ to thetotal num&er of darts thron is e2ual to the proportion of area under the cur$e to total area of

    the domain. ote that darts a&o$e the cur$e &ut &elo the x-axis su&tract from this total.

    noing that there are 6

  • 7/22/2019 numerical Methods with FreeMat

    30/40

    pass arguments to. 4e need to tell it three things9 our function *hich e can pass as a $aria&leusing an anonymous function/, the minimum x and the maximum x *the extrema of our domain/.

    function return&alue = montecarlof min ma'

    ndarts = 1##; % number of darts to throw; more darts = more accurate

    %% --- Determine Domain (ie --- %% = linspacemin ma ##';for n = 1Klen5th'

    ,n' = fn''; % draw cur&e to be inte5ratedend,min = min,';

    if ,min ) # % if , is all positi&e min is #,min = #;

    end

    ,ma = ma,';

    %% --- Chrow Darts6 --- %%dartcounter = #; % Chis will count the number of darts below the cur&efor i = 1Kndarts

    r = rand'0ma-min' - min; % 5enerates random dartr, = rand'0,ma-,min' - ,min;

    %% --- $ount Darts for $alculation --- %%if r, + fr' ** r, ) #

    dartcounter = dartcounter 1;else if r, ) fr' ** r, + #

    dartcounter = dartcounter - 1;end

    end

    end

    totalarea = absma-min'0abs,ma-,min';return&alue = dartcounter/ndarts'0totalarea; % area under cur&e

    o, let)s pass an anonymous function *a function stored in a $aria&le/ and a domain to ourprogram and see hat happens9

    --) f = 9' ep-.:'; % NEJO hard to inte5rate anal,ticall,6

    I)ll ta!e a 2uic! aside and explain hat I 5ust did 3 the name of our function *f/ can &e passedaround 5ust li!e any other $aria&le. 4hen e say f Q *x/, the sym&ol denotes that it)s afunction *not a $ector or cell or other entity/, and the *x/ denotes that it)s a function of x. If it erea function of x,y and ?, e ould ha$e f Q *x,y,?/. The rest of the expression is simply thefunction to &e e$aluated, in this case9

    fx =ex

    2

  • 7/22/2019 numerical Methods with FreeMat

    31/40

    4hen e call f*7/, for example, it ill return exp*-7W:/, or 6.:7=6x6

  • 7/22/2019 numerical Methods with FreeMat

    32/40

    Section 3: nitial !alue "ro#lems $%rdinary Differential Equations&

    0ro&a&ly one of the most significant applications of numerical techni2ues, especially inengineering, is e$aluation of differential e2uations. "any of today)s pursuits in engineering in$ol$esolution of ordinary differential e2uations *D')s/ and(or partial differential e2uations *0D')s/.Anything from dynamics and $i&rations to heat transfer, fluid mechanics and e$en

    electromagnetism can &e modeled using differential e2uations. "any times, hoe$er, thesee2uations pro$e either $ery difficult or $ery time consuming to sol$e analytically, so e turn tonumerical methods for 2uic!, approximate solutions.

    Application of numerical methods to differential e2uations can $ary in difficulty. ust as somedifferential e2uations are hard to sol$e *and some solutions are hard to compute e$en ith ananalytical solution1/, e often encounter difficulty in numerics as ell. Different criteria such as the#ipschit? Condition or the Courant-%reidrichs-#ey Condition exist as guides to the using thesemethods for solutions, and extensi$e use of numerical methods re2uires at the $ery least a &asicunderstanding of these. 4e on)t discuss them in detail here 3 e trust that if you)re using these

    methods for really specific or! that you)$e ta!en it upon yourself to learn hen to use hat. It)seasy to get models to pro$ide an anser. It)s less easy to ma!e sure that anser is accurate.

    The most common, simplest form of D' e as engineers generally encounter are initial $aluepro&lems *I0)s/. They are D')s for hich e !no starting $alues. %or example, if e ant todescri&e the acceleration of a car from rest, e ould li!ely descri&e it as an I0 &ecause e !noits initialproperties *position, speed and acceleration are at ?ero to start ith/. The methods hereall attempt to approximate a solution to the &asic e2uation

    y 't = ft , y t, f t0 = y0

    As e)ll see later, this single e2uation can translate to higher order e2uations *such as our car)sacceleration, or current in an #C circuit, or motion of a mass-spring-damper system, etc./ &y asimple extension of the or! in this chapter, hich e)ll discuss. #et)s start ith the most &asicmethod for differential e2uations, 'uler)s "ethod.

    !.1 - $uler(s Method

    'uler)s method ta!es a $ery simple approach to approximating solutions for I0)s. This method isidely regarded as too simplistic and not accurate enough for most serious uses, &ut it ma!es foreasy practice for some $ery similar functions that are really accurate.

    4e)ll s!ip some of the theory, &ut a long time ago, a Siss mathematician #eonhard 'uler*pronounced )oyler)/ performed a lot of calculation and analysis in numerical integration toproduce hat are no !non as the 'uler Approximations. Among these is one nota&le methodthat can &e used for a simple numerical solution of a &asic I0. 'ssentially, his approximationor!s as follos9 if e separate the domain e)re interested in into small time steps, using theinformation at the first time step and our unsolveddifferential e2uation, e can ma!e a guess athat the $alue ill &e at the next iteration. %or small enough time steps, e can continue this o$er

  • 7/22/2019 numerical Methods with FreeMat

    33/40

    the hole domain and get a reasona&le approximation of the actual solution.

    #et)s start ith a &asic differential e2uation 3 eton)s #a of Cooling says that my cup of coffeeill cool more 2uic!ly hen the difference &eteen its temperature and the air is large, and less2uic!ly as the temperature approaches the am&ient air temperature. In other ords, the change intemperature per unit time is proportional &y a constant, !, to the difference &eteen its current

    temperature and the air temperature. Assuming the air temperature remains constant, e cantranslate this into e2uations as follos9

    dT

    dt = kTair T

    T0=160[ F], Tair= 75[ F], k= 0.1 [ 1min]

    'uler)s iterati$e function loo!s li!e this9

    yn1 = yn hf tn, yn

    So, our first couple of iterations using 'uler)s method to pro$ide an estimate ould loo! li!e this9

    T1 = T0 hkTair T0

    T2 = T1 hkTair T1

    here h is the length of our time step. 'ssentially, e dra a straight line from our initial point at*t

  • 7/22/2019 numerical Methods with FreeMat

    34/40

    That)s it1 4e ha$e a numerical solution. 4e ha$e a nice smooth cur$e that ma!es sense 3 as thecoffee gets closer and closer to room temperature, it cools more sloly, and our initial conditionof 6>< is met. ne pro&lem though 3 run this four or fi$e times ith different $alues for h, and plotthe results. ou might get something that loo!s li!e this9

    As e ma!e our time step &igger, our solution mo$es aay from the actual solution *I)$e plottedthe exact analytical solution here 3 it)s the one most toards the top/. ur first guess at h *>seconds/ is small enough for the time scale e)re loo!ing at 3 this particular differential e2uationisn)t too sensiti$e. thers, though, can &e really sensiti$e, and so 'uler)s method doesn)t or! asell. Thus, in practice, e)ll use a &it more sophisticated method, the unge-utta.

  • 7/22/2019 numerical Methods with FreeMat

    35/40

    !.2 - Run&e-utta Methods

    This is the real deal. unge-utta "ethods are &y far the most commonly used methods in mostengineering applications today. They ere de$eloped around 6

  • 7/22/2019 numerical Methods with FreeMat

    36/40

    k1 = hkTair T0 = 0.10.175160 = 0.85

    k2 = hkTair [T1k1

    2 ] = 0.841

    yn1 = yn

    k1k2

    2

    = 160 0.846 = 159.154

    4e 5ust ta!e our !6$alue, plug it into our !:formula, and a$erage the to for the totalapproximation yn+6. otice that our final temperature change is a little different from hat 'uler)s"ethod *!6/ gi$es us. As e increase h *ma!e our time step &igger/, this difference generallyincreases. 4e ould also see &igger differences for more difficult functions, e$en hen using asmall h. Anyay, let)s rite a script1

    temp1' = 1>#; % initial coffee temp C#tair = !; % air temp Cair = .1; % coolin5 coefficienth = .1; % time step @1/minAt = @#KhK>#A; % one hourfor i = 1Klen5tht'-1 % we7re 5oin5 one etra step with temp

    1 = h00tair - tempi''; = h00tair - tempi'1/''';tempi1' = tempi' .!01';

    end

    0lot your results, and you)ll see a cur$e nearly exactly li!e e sa in 'uler)s "ethod. #oo!ing atour code, e can see that it)s $ery similar to the setup for 'uler)s "ethod, &ut e calculatetemp*i+6/ in a different ay. Also, e ha$e to call our function f*t,y/ se$eral times each iteration.As e mo$e to higher-order methods, it ill &e easier to name a function and call it, instead ofriting it out for each ! $alue. #et)s mo$e on to an =, and a more sophisticated example.

    The = or!s exactly li!e the : except for to points 3 there are more ! terms, and e eightour a$erage, traditionally in the middle. Technically, you can eight these hoe$er you li!e, &ut forthe most part, e use the eighting shon here. Kere)s our iterati$e function9

  • 7/22/2019 numerical Methods with FreeMat

    37/40

    k1 = hftn, yn

    k2 = hf tnh

    2, yn

    k1

    2

    k3 = hftnh

    2 , y n

    k22

    k4 = hf tnh , ynk3

    yn1 = yn k12 k22 k3k46

    otice that the first to terms are exactly the same as the :. The third term !7is calculatedexactly the same ay as !:, &ut ith !:as our y-$alue instead of !6. This is 5ust a refinement methodfor !:)s $alue. !=e$aluates y at *t+h/ using !7)s approximation for y, and then e ta!e a eighteda$erage here the middle $alues are more eighted than the ends. This is shon in %igure MX.

    4e can 2uic!ly implement this again in our script from &efore, 5ust as a demonstration9

    igure :( &; values for guess at single time ste. %he black line is theactual solution.

  • 7/22/2019 numerical Methods with FreeMat

    38/40

    temp1' = 1>#; % initial coffee temp C#tair = !; % air temp Cair = .1; % coolin5 coefficienth = .1; % time step @1/minAt = @#KhK>#A; % one hourfor i = 1Klen5tht'-1 % we7re 5oin5 one etra step with temp

    1 = h00tair - tempi''; = h00tair - tempi'1/''';3 = h00tair - tempi'/'''; = h00tair - tempi''';tempi1' = tempi' 1/>'01003';

    end

    eally 2uic!ly, let)s loo! at error. The exact solution of this e2uation is pretty easy to come &y9

    Tt = 85e kt75

    4e can run our three scripts, eulerex.m r!:.m and r!=.m, and put their $alues up against the real

    solution for a comparison. I 5ust too! h Q 6, and pic!ed a fe points o$er the domain9

    otice 'uler isn)t $ery accurate compared to the :, and li!eise on to the =. Kere)s a ta&le ofabsolute error *error that doesn)t ta!e into consideration the si?e of the $alues it)s comparing9

    These num&ers ill shrin! significantly if e ta!e h to &e smaller, &ut e don)t ant to aste time

    and memory. The lesson to ta!e aay here is that the more sophisticated model gi$es you *inmost cases/ a more accurate result. In a stiffer or less simple e2uation, this difference ould &egreatly magnified. o let)s ta!e a loo! at a more ad$anced program ith some real teeth.

    function ret = rftf#';

    ,1' = f#;

    for i = 1Klen5tht'-1

    $uler R2 R) $/act

    160.0000 160.0000 160.0000 160.0000

    151.5000 151.7125 151.9108 151.9112

    107.9307 108.7632 109.5570 109.5584

    86.4822 87.1036 87.7122 87.7133

    Error [F]

    $uler R2 R)

    0.0000 0.0000 0.0000

    0.4112 0.1987 0.0003

    1.6277 0.7952 0.0014

    1.2311 0.6097 0.0011

  • 7/22/2019 numerical Methods with FreeMat

    39/40

    h = ti1' 4 ti';

    %--- sol&es Jun5e-Putta P &alues ---%1 = h0fti',i''; = h0fti'.!0h,i'.!01';3 = h0fti'.!0h,i'.!0'; = h0fti'h,i'3';

    %--- sol&es for , from Jun5e-Putta coefficients ---%,i1' = ,i' 1/>'01003';

    end

    plott,'; % optional plot comment out if ,ou don7t want this ret = @t7 ,7A;

    end

    otice that this program is the same length as our pre$ious script, &ut it)s muchmore poerful.Osing this program, e can pass any function using an anonymous function *the f argument here/from the console, along ith a ro $ector of time $alues *t/, and our initial condition *f#A; % one hour--) p = 9t,' #.10!-,''; %our function 4 t is time , istemperature--) p# = 1>#; % our initial condition

    --) rptp#'

    4e get the exact same $alues e had &efore ith our = script, and a plot to match1 A couple ofnotes here9 hether our function is a function of &oth t and y or not, e need to define it as such.This test function doesn)t really care a&out t, 5ust y *hich is temperature, T in our case/ 3remem&er that t is included imlicitly&y using h in the calculation of each $alue for y. 4hen ourr!= program calls the function though, it ants to pass to arguments, time and temperature. 4ecan pass fewerarguments than are defined in a function, &ut not more. Therefore, e defined p Q*t,y/ instead of 5ust p Q *y/. The t $alue doesn)t do anything in our function, &ut r!= doesn)treally !no or care.

    !.! - Adams-Bashforth Methods

    !.) - "#stems of Differential $%uations

  • 7/22/2019 numerical Methods with FreeMat

    40/40

    "ection )0 Boundar# alue roblems

    ).1 - 3inear "hootin& Method

    The linear shooting method is a relati$ely simple approach to sol$ing a &oundary $alue pro&lem. In

    general, e !no conditions at to &oundaries, resulting in a more complex system than a simpleunge-utta can sol$e 3 e need to ta!e into account not only the initial $alue of the solution, &ut$alues at later points as ell. A physical e2ui$alent of a &oundary $alue pro&lem could &ethroing a &ase&all to a catcher, as shon in %igure MX. The throer ad5usts the speed of the &alland the angle at hich it)s thron to ma!e the &all land here they ant. In fact, the human &rainis possi&ly one of the fastest sol$ers for &oundary $alue pro&lems, in that it can sol$e thesesystems in an analog fashion in milliseconds, hether it)s mo$ing one)s hand to catch a &all, ormanually landing an aircraft on an aircraft carrier dec!.

    %or our purposes here, e)ll generally consider functions of the form9

    y 'x = fx , y x , fx0 = y0, fx1 = y1

    Kere, x< is the initial point and x6is the final point on the independent axis *usually denoting alength or displacement, depending on the pro&lem/. Continuing the example of a pitcher throinga &all, he or she !nos se$eral !ey pieces of information &efore throing the &all9 the initial heightof the &all *the position of the pitcher)s hand at release, x


Recommended