SlideShare ist ein Scribd-Unternehmen logo
1 von 9
Downloaden Sie, um offline zu lesen
AMTH250                        Assignment 4
       Due date: 27th August 2012
                Solutions

                                August 10, 2012


Question 1
The following data are related to the life expectation of citizens of two countries:
                                1975    1980    1985    1990
                        Tanab   72.8    74.2    75.2    76.4
                        Sarac   70.2    70.2    70.3    71.2

  1. Use the interpolating polynomial of degree three to estimate the life ex-
     pectation in 1977, 1983, and 1988.
  2. Repeat the computations using now the cubic spline interpolation.
  3. Compare the above two results.

octave:1> year=[1975 1980 1985 1990];
octave:2> tanab=[72.8 74.2 75.2 76.4];
octave:3> sarac=[70.2 70.2 70.3 71.2];
octave:4> ctanab=polyfit(year, tanab,3);
octave:5> csarac=polyfit(year, sarac,3);
octave:6> esttanab=polyval(ctanab, [1977 1983 1988])
esttanab =

   73.446      74.810     75.858

octave:7> estsarac=polyval(csarac, [1977 1983 1988])
estsarac =

   70.233      70.203     70.699


The estimated values in 1977, 1983 and 1988 are
   73.446 74.810 75.858 for Tanab, and
   70.233 70.203 70.699 for Sarac.




                                         1
Question 2
This is an interpolation problem. It solved by constructing the basis matrix and
solving a linear system.
octave:> x = [-2 -1 0 1 2]’;
octave:> y = [125.948 7.673 -4 -14.493 -103.429]’;
octave:> aa = [exp(-2*x) exp(-x) ones(5,1) exp(x) exp(2*x)];
octave:> coeffs = aay
coeffs =
   3.0000
  -4.9998
  -1.0004
   1.0003
  -2.0000

Thus the interpolating function is

                      y ≈ 3e−2x − 5e−x − 1 + ex − 2e2x .

Question 3
First we compute the interpolating functions:
octave:>   x = [0 0.01 0.04 0.09 0.16 0.25]’;
octave:>   y = [0 0.1 0.2 0.3 0.4 0.5]’;
octave:>   p = polyfit(x, y, 5);
octave:>   cs = interp1(x, y, ’spline’, ’pp’);
octave:>   pc = interp1(x, y, ’pchip’, ’pp’);
   Plotting the errors, i.e. the difference between the interpolating function
     √
and x we see that the pchip cubic is the most accurate over most domain.
   Restricting attention to the interval [0, 0.01] shows that they are all about
equally inaccurate over that range. The reason for this is that the derivative
   √
of x is infinite at x = 0 making approximation by a function with a finite
derivative at x = 0 difficult near x = 0.




                                       2
Polynomial                                                  Cubic Spline
 0.03                                                            0.03



 0.02                                                            0.02



 0.01                                                            0.01



    0                                                               0



 -0.01                                                           -0.01



 -0.02                                                           -0.02



 -0.03                                                           -0.03
         0   0.05     0.1            0.15     0.2    0.25                0   0.05     0.1         0.15     0.2    0.25




                        Pchip Cubic
 0.03



 0.02



 0.01



    0



 -0.01



 -0.02



 -0.03
         0   0.05     0.1            0.15     0.2    0.25




               Figure 1: Errors in interpolating functions.



                            Polynomial                                                  Cubic Spline
    0                                                               0



-0.005                                                          -0.005



 -0.01                                                           -0.01



-0.015                                                          -0.015



 -0.02                                                           -0.02



-0.025                                                          -0.025
         0   0.002   0.004           0.006   0.008   0.01                0   0.002   0.004        0.006   0.008   0.01




                        Pchip Cubic
    0



-0.005



 -0.01



-0.015



 -0.02



-0.025
         0   0.002   0.004           0.006   0.008   0.01




     Figure 2: Errors in interpolating functions in [0, 0.01].




                                                            3
Question 4


                             Cubic Spline                                       1st Derivative
         12                                                    3



         10
                                                               2


          8
                                                               1

          6

                                                               0
          4


                                                               -1
          2



          0                                                    -2
               1   2     3         4          5   6   7             1   2   3         4          5   6   7




                             2nd Derivative                                     3rd Derivative
          8                                                    8


                                                               6
          6

                                                               4
          4

                                                               2
          2
                                                               0

          0
                                                               -2

          -2
                                                               -4


          -4                                                   -6
               1   2     3         4          5   6   7             1   2   3         4          5   6   7




                       Figure 3: Cubic spline and its derivatives.



                              Pchip Cubic                                       1st Derivative
         12                                                    3



         10                                                   2.5



          8                                                    2



          6                                                   1.5



          4                                                    1



          2                                                   0.5



          0                                                    0
               1   2     3         4          5   6   7             1   2   3         4          5   6   7




                             2nd Derivative                                     3rd Derivative
         10                                                    5




          5                                                    0




          0                                                    -5




          -5                                                  -10




         -10                                                  -15
               1   2     3         4          5   6   7             1   2   3         4          5   6   7




                       Figure 4: Pchip cubic and its derivatives.




                                                          4
From the graphs we see the defining conditions for a cubic spline are satisfied:

  1. From the first plot the function interpolates the data.
  2. From the second and third plots the function has continuous first and
     second derivatives.
  3. From the fourth plot the function has constant third derivative on each
     subinterval. It follows that the function is a cubic polynomial on each
     subinterval.
  4. The ‘not-a-knot’ condition shows up in the third derivative is the same on
     the first and second subintervals and on the second last and last subinter-
     vals.

   The defining conditions for a pchip cubic are also satisfied:

  1. From the first plot the function interpolates the data.
  2. From the second plot the function has continuous first derivative. The
     third plot shows that the second derivative is discontinuous.
  3. From the fourth plot the function has constant third derivative on each
     subinterval. It follows that the function is a cubic polynomial on each
     subinterval.
  4. In this example the data is monotonic, a property shared by the pchip
     cubic. This implies that the derivative of the pchip cubic does not change
     sign. The second plot shows that derivative is everywhere positive.

Question 5
Write the equations in matrix form and solve the least squares problem with
the backslash operator.
octave:> A = [1 0 0 0
>0 1 0 0
>0 0 1 0
>0 0 0 1
>1 -1 0 0
>1 0 -1 0
>1 0 0 -1
>0 1 -1 0
>0 1 0 -1
>0 0 1 -1];
octave:> y = [2.95 1.74 -1.45 1.32 1.23 4.45 1.61 3.21 0.45 -2.75]’;
octave:> x = Ay
x =
   2.9600
   1.7460
  -1.4600
   1.3140
These values are within ±0.01 of the direct measurements.


                                       5
Question 6
Here is a script to compute and plot the fitted functions:
x = 1:10;
y = [27.7 39.3 38.4 57.6 46.3 54.8 108.5 137.6 194.1 281.2];
lx =log(x);
ly =log(y);
p = polyfit(x, y, 2);
ce = polyfit(x, ly, 1);
ae = exp(ce(2));
ke = ce(1);
fe = @(x) ae*exp(ke*x);
cp = polyfit(lx, ly, 1);
ap = exp(cp(2));
kp = cp(1);
fp = @(x) ap*x.^kp;

clf()
xx = linspace(1, 10, 201);
plot(x,y,’or’)
hold on
plot(xx, polyval(p,xx))
plot(xx, fe(xx),’k’)
plot(xx, fp(xx), ’g’)
hold off


           300
                                                                   Quadratic
                                                         Exponential Function
                                                             Power Function




           250




           200




           150




           100




            50




             0
                 0      2          4           6           8                    10




     From this we see that the power function gives a poor fit, but visually there
is little to chose between the quadratic polynomial and the exponential function.


                                       6
Part of the reason the power function y = axp gives such a poor fit is that when
p > 0 the graph of the function must pass through the origin.
   Plotting the residuals
res1 = y - polyval(p,x);
res2 = y - fe(x);
res3 = y - fp(x);
Shows that the quadratic polynomial has the smallest residuals. This is con-
firmed by the norms
octave:> norm(res1)
ans = 43.364
octave:> norm(res2)
ans = 70.894
octave:> norm(res3)
ans = 151.69


           150
                                                                   Quadratic
                                                         Exponential Function
                                                             Power Function




           100




           50




            0




           -50
                 0      2          4           6           8                    10




                     Figure 5: Residuals of fitted functions.


   A semilog plot of the data indicates that an exponential function function
may give a good description of the data. When working with the semilog data,
the appropriate residuals are the differences log yi − log f (xi )
lres1 = ly - log(polyval(p,x));
lres2 = ly - log(fe(x));
lres3 = ly - log(fp(x));
   Using the semilog scale that the exponential function has the smallest resid-
uals. This is confirmed by the norms


                                       7
octave:> norm(lres1)
ans = 0.87342
octave:> norm(lres2)
ans = 0.66707
octave:> norm(lres3)
ans = 1.1785


                                  Fitted Functions                                          Semilog Residuals
           6                                                             0.8
                                                   Quadratic                                                  Quadratic
                                         Exponential Function                                       Exponential Function
                                             Power Function                                             Power Function


                                                                         0.6
          5.5




                                                                         0.4

           5




                                                                         0.2


          4.5



                                                                           0



           4


                                                                         -0.2




          3.5

                                                                         -0.4




           3
                                                                         -0.6




          2.5                                                            -0.8
                0   2             4             6          8    10              0   2        4            6          8     10




       Figure 6: Semilog plot of fitted functions and semilog residuals.


   A log-log plot of the data indicates that a power function would not be
expected to give a good fit to the data.

                         6




                        5.5




                         5




                        4.5




                         4




                        3.5




                         3
                              0                      0.5        1           1.5         2                 2.5




                                  Figure 7: Log-log plot of the data



                                                                     8
In summary
1. The power function does not give a good representation of the data.

2. The quadratic polynomial and exponential function give reasonable rep-
   resentations of the data.
3. Examining the residuals would indicate that the quadratic gives the best
   representation.

4. A semilog plot indicates that an exponential function may give a good
   representation of the data. Another reason for preferring the exponential
   is that it is monotonic and we may believe from examining the data that
   the underlying trend is monotonic. The exponential function is monotonic,
   the quadratic polynomial is not.




                                    9

Weitere ähnliche Inhalte

Was ist angesagt?

Modul matematik( panjang, berat, isipadu cecair)
Modul matematik( panjang, berat, isipadu cecair)Modul matematik( panjang, berat, isipadu cecair)
Modul matematik( panjang, berat, isipadu cecair)
soulmoon
 
Regulations As a "Panacea": Exploring the Consequences
Regulations As a "Panacea": Exploring the ConsequencesRegulations As a "Panacea": Exploring the Consequences
Regulations As a "Panacea": Exploring the Consequences
Mercatus Center
 

Was ist angesagt? (16)

Small Business Energy Efficiency: Roadmap to Program Design
Small Business Energy Efficiency: Roadmap to Program DesignSmall Business Energy Efficiency: Roadmap to Program Design
Small Business Energy Efficiency: Roadmap to Program Design
 
7 quality tools
7 quality tools7 quality tools
7 quality tools
 
Modul matematik( panjang, berat, isipadu cecair)
Modul matematik( panjang, berat, isipadu cecair)Modul matematik( panjang, berat, isipadu cecair)
Modul matematik( panjang, berat, isipadu cecair)
 
Fxdq
FxdqFxdq
Fxdq
 
Copy Of Consumer
Copy Of ConsumerCopy Of Consumer
Copy Of Consumer
 
Ukázkový report - SolidVision
Ukázkový report - SolidVisionUkázkový report - SolidVision
Ukázkový report - SolidVision
 
2009 Face Conference Ma Fiore
2009 Face Conference Ma Fiore2009 Face Conference Ma Fiore
2009 Face Conference Ma Fiore
 
Regulations As a "Panacea": Exploring the Consequences
Regulations As a "Panacea": Exploring the ConsequencesRegulations As a "Panacea": Exploring the Consequences
Regulations As a "Panacea": Exploring the Consequences
 
FY07 Master .xls
FY07 Master .xlsFY07 Master .xls
FY07 Master .xls
 
Mlb graphs slide deck
Mlb graphs slide deckMlb graphs slide deck
Mlb graphs slide deck
 
credit-suisse Restatement re
credit-suisse Restatement recredit-suisse Restatement re
credit-suisse Restatement re
 
VaR of Operational Risk
VaR of Operational RiskVaR of Operational Risk
VaR of Operational Risk
 
histograma
histograma histograma
histograma
 
Laboratorio valoración de cartera resolución enviar
Laboratorio valoración de cartera resolución enviarLaboratorio valoración de cartera resolución enviar
Laboratorio valoración de cartera resolución enviar
 
251109 Or
251109 Or251109 Or
251109 Or
 
Ejercicio 9
Ejercicio 9Ejercicio 9
Ejercicio 9
 

Andere mochten auch

Amth250 octave matlab some solutions (1)
Amth250 octave matlab some solutions (1)Amth250 octave matlab some solutions (1)
Amth250 octave matlab some solutions (1)
asghar123456
 
Amth250 octave matlab some solutions (4)
Amth250 octave matlab some solutions (4)Amth250 octave matlab some solutions (4)
Amth250 octave matlab some solutions (4)
asghar123456
 
Answers assignment 3 integral methods-fluid mechanics
Answers assignment 3 integral methods-fluid mechanicsAnswers assignment 3 integral methods-fluid mechanics
Answers assignment 3 integral methods-fluid mechanics
asghar123456
 
Answers assignment 4 real fluids-fluid mechanics
Answers assignment 4 real fluids-fluid mechanicsAnswers assignment 4 real fluids-fluid mechanics
Answers assignment 4 real fluids-fluid mechanics
asghar123456
 
Answers assignment 2 fluid statics-fluid mechanics
Answers assignment 2 fluid statics-fluid mechanicsAnswers assignment 2 fluid statics-fluid mechanics
Answers assignment 2 fluid statics-fluid mechanics
asghar123456
 
Answers assignment 5 open channel hydraulics-fluid mechanics
Answers assignment 5 open channel hydraulics-fluid mechanicsAnswers assignment 5 open channel hydraulics-fluid mechanics
Answers assignment 5 open channel hydraulics-fluid mechanics
asghar123456
 

Andere mochten auch (6)

Amth250 octave matlab some solutions (1)
Amth250 octave matlab some solutions (1)Amth250 octave matlab some solutions (1)
Amth250 octave matlab some solutions (1)
 
Amth250 octave matlab some solutions (4)
Amth250 octave matlab some solutions (4)Amth250 octave matlab some solutions (4)
Amth250 octave matlab some solutions (4)
 
Answers assignment 3 integral methods-fluid mechanics
Answers assignment 3 integral methods-fluid mechanicsAnswers assignment 3 integral methods-fluid mechanics
Answers assignment 3 integral methods-fluid mechanics
 
Answers assignment 4 real fluids-fluid mechanics
Answers assignment 4 real fluids-fluid mechanicsAnswers assignment 4 real fluids-fluid mechanics
Answers assignment 4 real fluids-fluid mechanics
 
Answers assignment 2 fluid statics-fluid mechanics
Answers assignment 2 fluid statics-fluid mechanicsAnswers assignment 2 fluid statics-fluid mechanics
Answers assignment 2 fluid statics-fluid mechanics
 
Answers assignment 5 open channel hydraulics-fluid mechanics
Answers assignment 5 open channel hydraulics-fluid mechanicsAnswers assignment 5 open channel hydraulics-fluid mechanics
Answers assignment 5 open channel hydraulics-fluid mechanics
 

Ähnlich wie Amth250 octave matlab some solutions (3)

adc converter basics
adc converter basicsadc converter basics
adc converter basics
hacker1500
 
Las ม.2 ปีการศึกษา 2554
Las ม.2 ปีการศึกษา 2554Las ม.2 ปีการศึกษา 2554
Las ม.2 ปีการศึกษา 2554
Aobinta In
 
Frequency vs community impact
Frequency vs community impactFrequency vs community impact
Frequency vs community impact
holdsteady
 
Cornah and vann 2012 non mg multivariate cs - presentation
Cornah and vann 2012 non mg multivariate cs - presentationCornah and vann 2012 non mg multivariate cs - presentation
Cornah and vann 2012 non mg multivariate cs - presentation
Alastair Cornah
 
Transformer design
Transformer designTransformer design
Transformer design
sulaim_qais
 
Toi dua em sang song
Toi dua em sang songToi dua em sang song
Toi dua em sang song
hoangmeo60
 

Ähnlich wie Amth250 octave matlab some solutions (3) (20)

adc converter basics
adc converter basicsadc converter basics
adc converter basics
 
Machine learning projects with r
Machine learning projects with rMachine learning projects with r
Machine learning projects with r
 
Metrado de madera
Metrado de maderaMetrado de madera
Metrado de madera
 
Excel slide series - fractions introduction
Excel slide series -  fractions introductionExcel slide series -  fractions introduction
Excel slide series - fractions introduction
 
Las ม.2 ปีการศึกษา 2554
Las ม.2 ปีการศึกษา 2554Las ม.2 ปีการศึกษา 2554
Las ม.2 ปีการศึกษา 2554
 
Frequency vs community impact
Frequency vs community impactFrequency vs community impact
Frequency vs community impact
 
Cornah and vann 2012 non mg multivariate cs - presentation
Cornah and vann 2012 non mg multivariate cs - presentationCornah and vann 2012 non mg multivariate cs - presentation
Cornah and vann 2012 non mg multivariate cs - presentation
 
Characteristics of the kinase mutant TPK2 in bioreactors
Characteristics of the kinase mutant TPK2 in bioreactorsCharacteristics of the kinase mutant TPK2 in bioreactors
Characteristics of the kinase mutant TPK2 in bioreactors
 
DCT_TR802
DCT_TR802DCT_TR802
DCT_TR802
 
DCT_TR802
DCT_TR802DCT_TR802
DCT_TR802
 
DCT_TR802
DCT_TR802DCT_TR802
DCT_TR802
 
Education 3.0: Better Learning Through Technology
Education 3.0:  Better Learning Through TechnologyEducation 3.0:  Better Learning Through Technology
Education 3.0: Better Learning Through Technology
 
Transformer design
Transformer designTransformer design
Transformer design
 
LinkedIn’s Q4 2011 Earnings Announcement
LinkedIn’s Q4 2011 Earnings AnnouncementLinkedIn’s Q4 2011 Earnings Announcement
LinkedIn’s Q4 2011 Earnings Announcement
 
Toi dua em sang song
Toi dua em sang songToi dua em sang song
Toi dua em sang song
 
09 trial jpwp_s2
09 trial jpwp_s209 trial jpwp_s2
09 trial jpwp_s2
 
Brian Bonnenfants Nevada Business Outlook 1 23 09
Brian Bonnenfants Nevada Business Outlook 1 23 09Brian Bonnenfants Nevada Business Outlook 1 23 09
Brian Bonnenfants Nevada Business Outlook 1 23 09
 
鹿沢万座通信25号
鹿沢万座通信25号鹿沢万座通信25号
鹿沢万座通信25号
 
iGridd puzzles, Demo book
iGridd puzzles, Demo bookiGridd puzzles, Demo book
iGridd puzzles, Demo book
 
Presenting objective and subjective uncertainty information for spatial syste...
Presenting objective and subjective uncertainty information for spatial syste...Presenting objective and subjective uncertainty information for spatial syste...
Presenting objective and subjective uncertainty information for spatial syste...
 

Mehr von asghar123456

assignment 1 properties of fluids-Fluid mechanics
assignment 1 properties of fluids-Fluid mechanicsassignment 1 properties of fluids-Fluid mechanics
assignment 1 properties of fluids-Fluid mechanics
asghar123456
 
Buckling test engt110
Buckling test engt110Buckling test engt110
Buckling test engt110
asghar123456
 
Amth250 octave matlab some solutions (2)
Amth250 octave matlab some solutions (2)Amth250 octave matlab some solutions (2)
Amth250 octave matlab some solutions (2)
asghar123456
 

Mehr von asghar123456 (6)

assignment 1 properties of fluids-Fluid mechanics
assignment 1 properties of fluids-Fluid mechanicsassignment 1 properties of fluids-Fluid mechanics
assignment 1 properties of fluids-Fluid mechanics
 
Buckling test engt110
Buckling test engt110Buckling test engt110
Buckling test engt110
 
Assignment6
Assignment6Assignment6
Assignment6
 
Assignment5
Assignment5Assignment5
Assignment5
 
Assignment4
Assignment4Assignment4
Assignment4
 
Amth250 octave matlab some solutions (2)
Amth250 octave matlab some solutions (2)Amth250 octave matlab some solutions (2)
Amth250 octave matlab some solutions (2)
 

Kürzlich hochgeladen

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Krashi Coaching
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 

Kürzlich hochgeladen (20)

Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 

Amth250 octave matlab some solutions (3)

  • 1. AMTH250 Assignment 4 Due date: 27th August 2012 Solutions August 10, 2012 Question 1 The following data are related to the life expectation of citizens of two countries: 1975 1980 1985 1990 Tanab 72.8 74.2 75.2 76.4 Sarac 70.2 70.2 70.3 71.2 1. Use the interpolating polynomial of degree three to estimate the life ex- pectation in 1977, 1983, and 1988. 2. Repeat the computations using now the cubic spline interpolation. 3. Compare the above two results. octave:1> year=[1975 1980 1985 1990]; octave:2> tanab=[72.8 74.2 75.2 76.4]; octave:3> sarac=[70.2 70.2 70.3 71.2]; octave:4> ctanab=polyfit(year, tanab,3); octave:5> csarac=polyfit(year, sarac,3); octave:6> esttanab=polyval(ctanab, [1977 1983 1988]) esttanab = 73.446 74.810 75.858 octave:7> estsarac=polyval(csarac, [1977 1983 1988]) estsarac = 70.233 70.203 70.699 The estimated values in 1977, 1983 and 1988 are 73.446 74.810 75.858 for Tanab, and 70.233 70.203 70.699 for Sarac. 1
  • 2. Question 2 This is an interpolation problem. It solved by constructing the basis matrix and solving a linear system. octave:> x = [-2 -1 0 1 2]’; octave:> y = [125.948 7.673 -4 -14.493 -103.429]’; octave:> aa = [exp(-2*x) exp(-x) ones(5,1) exp(x) exp(2*x)]; octave:> coeffs = aay coeffs = 3.0000 -4.9998 -1.0004 1.0003 -2.0000 Thus the interpolating function is y ≈ 3e−2x − 5e−x − 1 + ex − 2e2x . Question 3 First we compute the interpolating functions: octave:> x = [0 0.01 0.04 0.09 0.16 0.25]’; octave:> y = [0 0.1 0.2 0.3 0.4 0.5]’; octave:> p = polyfit(x, y, 5); octave:> cs = interp1(x, y, ’spline’, ’pp’); octave:> pc = interp1(x, y, ’pchip’, ’pp’); Plotting the errors, i.e. the difference between the interpolating function √ and x we see that the pchip cubic is the most accurate over most domain. Restricting attention to the interval [0, 0.01] shows that they are all about equally inaccurate over that range. The reason for this is that the derivative √ of x is infinite at x = 0 making approximation by a function with a finite derivative at x = 0 difficult near x = 0. 2
  • 3. Polynomial Cubic Spline 0.03 0.03 0.02 0.02 0.01 0.01 0 0 -0.01 -0.01 -0.02 -0.02 -0.03 -0.03 0 0.05 0.1 0.15 0.2 0.25 0 0.05 0.1 0.15 0.2 0.25 Pchip Cubic 0.03 0.02 0.01 0 -0.01 -0.02 -0.03 0 0.05 0.1 0.15 0.2 0.25 Figure 1: Errors in interpolating functions. Polynomial Cubic Spline 0 0 -0.005 -0.005 -0.01 -0.01 -0.015 -0.015 -0.02 -0.02 -0.025 -0.025 0 0.002 0.004 0.006 0.008 0.01 0 0.002 0.004 0.006 0.008 0.01 Pchip Cubic 0 -0.005 -0.01 -0.015 -0.02 -0.025 0 0.002 0.004 0.006 0.008 0.01 Figure 2: Errors in interpolating functions in [0, 0.01]. 3
  • 4. Question 4 Cubic Spline 1st Derivative 12 3 10 2 8 1 6 0 4 -1 2 0 -2 1 2 3 4 5 6 7 1 2 3 4 5 6 7 2nd Derivative 3rd Derivative 8 8 6 6 4 4 2 2 0 0 -2 -2 -4 -4 -6 1 2 3 4 5 6 7 1 2 3 4 5 6 7 Figure 3: Cubic spline and its derivatives. Pchip Cubic 1st Derivative 12 3 10 2.5 8 2 6 1.5 4 1 2 0.5 0 0 1 2 3 4 5 6 7 1 2 3 4 5 6 7 2nd Derivative 3rd Derivative 10 5 5 0 0 -5 -5 -10 -10 -15 1 2 3 4 5 6 7 1 2 3 4 5 6 7 Figure 4: Pchip cubic and its derivatives. 4
  • 5. From the graphs we see the defining conditions for a cubic spline are satisfied: 1. From the first plot the function interpolates the data. 2. From the second and third plots the function has continuous first and second derivatives. 3. From the fourth plot the function has constant third derivative on each subinterval. It follows that the function is a cubic polynomial on each subinterval. 4. The ‘not-a-knot’ condition shows up in the third derivative is the same on the first and second subintervals and on the second last and last subinter- vals. The defining conditions for a pchip cubic are also satisfied: 1. From the first plot the function interpolates the data. 2. From the second plot the function has continuous first derivative. The third plot shows that the second derivative is discontinuous. 3. From the fourth plot the function has constant third derivative on each subinterval. It follows that the function is a cubic polynomial on each subinterval. 4. In this example the data is monotonic, a property shared by the pchip cubic. This implies that the derivative of the pchip cubic does not change sign. The second plot shows that derivative is everywhere positive. Question 5 Write the equations in matrix form and solve the least squares problem with the backslash operator. octave:> A = [1 0 0 0 >0 1 0 0 >0 0 1 0 >0 0 0 1 >1 -1 0 0 >1 0 -1 0 >1 0 0 -1 >0 1 -1 0 >0 1 0 -1 >0 0 1 -1]; octave:> y = [2.95 1.74 -1.45 1.32 1.23 4.45 1.61 3.21 0.45 -2.75]’; octave:> x = Ay x = 2.9600 1.7460 -1.4600 1.3140 These values are within ±0.01 of the direct measurements. 5
  • 6. Question 6 Here is a script to compute and plot the fitted functions: x = 1:10; y = [27.7 39.3 38.4 57.6 46.3 54.8 108.5 137.6 194.1 281.2]; lx =log(x); ly =log(y); p = polyfit(x, y, 2); ce = polyfit(x, ly, 1); ae = exp(ce(2)); ke = ce(1); fe = @(x) ae*exp(ke*x); cp = polyfit(lx, ly, 1); ap = exp(cp(2)); kp = cp(1); fp = @(x) ap*x.^kp; clf() xx = linspace(1, 10, 201); plot(x,y,’or’) hold on plot(xx, polyval(p,xx)) plot(xx, fe(xx),’k’) plot(xx, fp(xx), ’g’) hold off 300 Quadratic Exponential Function Power Function 250 200 150 100 50 0 0 2 4 6 8 10 From this we see that the power function gives a poor fit, but visually there is little to chose between the quadratic polynomial and the exponential function. 6
  • 7. Part of the reason the power function y = axp gives such a poor fit is that when p > 0 the graph of the function must pass through the origin. Plotting the residuals res1 = y - polyval(p,x); res2 = y - fe(x); res3 = y - fp(x); Shows that the quadratic polynomial has the smallest residuals. This is con- firmed by the norms octave:> norm(res1) ans = 43.364 octave:> norm(res2) ans = 70.894 octave:> norm(res3) ans = 151.69 150 Quadratic Exponential Function Power Function 100 50 0 -50 0 2 4 6 8 10 Figure 5: Residuals of fitted functions. A semilog plot of the data indicates that an exponential function function may give a good description of the data. When working with the semilog data, the appropriate residuals are the differences log yi − log f (xi ) lres1 = ly - log(polyval(p,x)); lres2 = ly - log(fe(x)); lres3 = ly - log(fp(x)); Using the semilog scale that the exponential function has the smallest resid- uals. This is confirmed by the norms 7
  • 8. octave:> norm(lres1) ans = 0.87342 octave:> norm(lres2) ans = 0.66707 octave:> norm(lres3) ans = 1.1785 Fitted Functions Semilog Residuals 6 0.8 Quadratic Quadratic Exponential Function Exponential Function Power Function Power Function 0.6 5.5 0.4 5 0.2 4.5 0 4 -0.2 3.5 -0.4 3 -0.6 2.5 -0.8 0 2 4 6 8 10 0 2 4 6 8 10 Figure 6: Semilog plot of fitted functions and semilog residuals. A log-log plot of the data indicates that a power function would not be expected to give a good fit to the data. 6 5.5 5 4.5 4 3.5 3 0 0.5 1 1.5 2 2.5 Figure 7: Log-log plot of the data 8
  • 9. In summary 1. The power function does not give a good representation of the data. 2. The quadratic polynomial and exponential function give reasonable rep- resentations of the data. 3. Examining the residuals would indicate that the quadratic gives the best representation. 4. A semilog plot indicates that an exponential function may give a good representation of the data. Another reason for preferring the exponential is that it is monotonic and we may believe from examining the data that the underlying trend is monotonic. The exponential function is monotonic, the quadratic polynomial is not. 9