]> Exercises

Exercises

Please complete each exercise or answer the question before reviewing the posted solution comments.

  1. Modify the code that you were given (in Team Lab 8) to approximate derivatives using Simpson's method. The new version should work when the function is passed as the first argument to the ashlee function.

    You must modify the function headers and the ashlee and jessica functions as indicated in bold in the following code:

    function lipSync_sol = ashlee( f_x, a, b, N)
    %  Compute the integral of the function f(x)
    %  between the limits a and b using Simpson's Method
    %  with N subintervals. N must be an even integer and
    %  b > a.
    %  oj is a function that computes the coefficients
    %  1, 2, 4, 2, 4, 2, ..., 1
    %  jessica is a function that computes f(xn)'s.
    h = (b-a) / N;
    HomerSimpson_sum = 0;
    for n_count=1:N+1
        n = n_count - 1;
        HomerSimpson_sum = HomerSimpson_sum + oj(n,N) * jessica(f_x,n,a,b,N);
    end
    lipSync_sol = (b-a)/(3*N) * HomerSimpson_sum;
    
    
    function coefficient = oj(n_input,N_input)
    %  Compute the Simpson's Method coefficients 1,2,4,2,4,2,...1
    if n_input == 0 | n_input == N_input
        coefficient = 1;
        return
    end
    if rem(n_input,2) == 0   % If n is even then coefficient = 2
        coefficient = 2;
        return
    end
        coefficient = 4;    % If n is odd then coefficient = 4
    
    
    function f_value = jessica(f_x, n, a, b, N)
    %  jessica is a function that computes f(x) at the x value
    %  specified by the index n, and the subinterval specified by
    %  a, b and N.
    f = fcnchk(f_x);
    x = a + n * (b-a)/N;
    f_value = f(x);
    
  2. Use your modified function(s) from exercise #1 to calculate and return the area approximation of f(x)= x 2 +10 MathType@MTEF@5@5@+=feaafiart1ev1aaatCvAUfeBSjuyZL2yd9gzLbvyNv2CaerbuLwBLnhiov2DGi1BTfMBaeXatLxBI9gBaerbd9wDYLwzYbItLDharqqr1ngBPrgifHhDYfgasaacH8YjY=vipgYlh9vqqj=hEeeu0xXdbba9frFj0=OqFfea0dXdd9vqai=hGuQ8kuc9pgc9q8qqaq=dir=f0=yqaiVgFr0xfr=xfr=xb9adbaqaaeGaciGaaiaabeqaamaabaabaaGcbaGaamOzaiaacIcacaWG4bGaaiykaiabg2da9iabgkHiTiaadIhadaahaaWcbeqaaiaaikdaaaGccqGHRaWkcaaIXaGaaGimaaaa@4112@ from 0 to 4 using 32 intervals.

    The following command should work when executed in the command window:

        >> area0_4 = ashlee( '-x^2+10', 0, 4, 32 )
    
        area0_4 =
    
           18.6667
  3. Define a new function named f3 in a file named f3.m that calculates and returns the value of this function: f3(x)=3 x 3 4 x 2 +5 MathType@MTEF@5@5@+=feaafiart1ev1aaatCvAUfeBSjuyZL2yd9gzLbvyNv2CaerbuLwBLnhiov2DGi1BTfMBaeXatLxBI9gBaerbd9wDYLwzYbItLDharqqr1ngBPrgifHhDYfgasaacH8YjY=vipgYlh9vqqj=hEeeu0xXdbba9frFj0=OqFfea0dXdd9vqai=hGuQ8kuc9pgc9q8qqaq=dir=f0=yqaiVgFr0xfr=xfr=xb9adbaqaaeGaciGaaiaabeqaamaabaabaaGcbaGaamOzaiaaiodaciGGOaGaamiEaiaacMcacqGH9aqpcaaIZaGaamiEamaaCaaaleqabaGaaG4maaaakiabgkHiTiaaisdacaWG4bWaaWbaaSqabeaacaaIYaaaaOGaey4kaSIaaGynaaaa@4487@

    Use your modified function(s) from exercise #1 to calculate and return the area approximation from 0 to 5 using 8 intervals.

    Also, calculate this area using Matlab's quad command to compare.

    The following commands should work when executed in the command window:

        >> area_f3 = ashlee( 'f3', 0, 5, 8 )
    
        area_f3 =
    
          327.0833
    
        >> quad( 'f3' , 0 , 5 )
    
        ans =
    
          327.0833
    
    >>