Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Let f(x) = 1/ 1 + x and g(x) = sinx. a ) Write a Matlab code to compute f(0) and

ID: 3167971 • Letter: L

Question

Let f(x) = 1/ 1 + x and g(x) = sinx. a ) Write a Matlab code to compute f(0) and g(0) using D+f := (f(x + h)f(x))/h and D0f := (f(x + h)f(xh))/2h and step-size h = 2n, n = 1,2,....,5. For each dierence formula (D+f and D0f) and each function (f and g), the code should generate a table which contains the following information column 1: h column 2: Df column 3: f(0)Df column 4: (f(0)Df)/h column 5: (f(0)Df)/h2 where D is being D+f and D0f. b ) Explain what is happening in each column. Let f(x) = 1/ 1 + x and g(x) = sinx. a ) Write a Matlab code to compute f(0) and g(0) using D+f := (f(x + h)f(x))/h and D0f := (f(x + h)f(xh))/2h and step-size h = 2n, n = 1,2,....,5. For each dierence formula (D+f and D0f) and each function (f and g), the code should generate a table which contains the following information column 1: h column 2: Df column 3: f(0)Df column 4: (f(0)Df)/h column 5: (f(0)Df)/h2 where D is being D+f and D0f. b ) Explain what is happening in each column. Let f(x) = 1/ 1 + x and g(x) = sinx. a ) Write a Matlab code to compute f(0) and g(0) using D+f := (f(x + h)f(x))/h and D0f := (f(x + h)f(xh))/2h and step-size h = 2n, n = 1,2,....,5. For each dierence formula (D+f and D0f) and each function (f and g), the code should generate a table which contains the following information column 1: h column 2: Df column 3: f(0)Df column 4: (f(0)Df)/h column 5: (f(0)Df)/h2 where D is being D+f and D0f. b ) Explain what is happening in each column.

Explanation / Answer

clc;
clear all;
f=@(x)1/(1+x);
df=@(x)-1/(1+x)^2;
n=5;
x=0;
for i=1:n
h(i)=1/2^i;
end
for i=1:n
    df_plus(i)=(f(x+h(i))-f(x))/h(i);
  
    df_zero(i)=(f(x+h(i))-f(x-h(i)))/(2*h(i));
end
disp('______________________________________________________________________________')
disp('h                  D+f            df(0)-D+f        (df(0)-D+f)/h       (df(0)-D+f)/h^2')
disp('______________________________________________________________________________')
   for i=1:n
       fprintf('%f %15f %15f %15f %15f ',h(i),df_plus(i),df(x)-df_plus(i),(df(x)-df_plus(i))/h(i),(df(x)-df_plus(i))/h(i)^2)
   end
   disp('______________________________________________________________________________')
    disp('h                 D0f            df(0)-D0f        (df(0)-D0f)/h       (df(0)-D0f)/h^2')
    disp('_______________________________________________________________________________')
    for i=1:n
       fprintf('%f %15f %15f %15f %15f ',h(i),df_plus(i),df(x)-df_zero(i),(df(x)-df_zero(i))/h(i),(df(x)-df_zero(i))/h(i)^2)
   end

_____________________________________________________________________________
h                  D+f            df(0)-D+f        (df(0)-D+f)/h       (df(0)-D+f)/h^2
______________________________________________________________________________
0.500000          -0.666667          -0.333333          -0.666667           -1.333333
0.250000          -0.800000          -0.200000          -0.800000           -3.200000
0.125000          -0.888889          -0.111111          -0.888889           -7.111111
0.062500          -0.941176          -0.058824          -0.941176          -15.058824
0.031250          -0.969697          -0.030303          -0.969697          -31.030303
______________________________________________________________________________
h                 D0f            df(0)-D0f        (df(0)-D0f)/h       (df(0)-D0f)/h^2
_______________________________________________________________________________
0.500000          -0.666667           0.333333           0.666667            1.333333
0.250000          -0.800000           0.066667           0.266667            1.066667
0.125000          -0.888889           0.015873           0.126984            1.015873
0.062500          -0.941176           0.003922           0.062745            1.003922
0.031250          -0.969697           0.000978           0.031281            1.000978

for g(x)

clc;
clear all;
g=@(x)sin(x);
dg=@(x)cos(x);
n=5;
x=0;
for i=1:n
h(i)=1/2^i;
end
for i=1:n
    dg_plus(i)=(g(x+h(i))-g(x))/h(i);
  
    dg_zero(i)=(g(x+h(i))-g(x-h(i)))/(2*h(i));
end
disp('______________________________________________________________________________')
disp('h                  D+g            dg(0)-D+g        (dg(0)-D+g)/h       (dg(0)-D+g)/h^2')
disp('______________________________________________________________________________')
   for i=1:n
       fprintf('%f %15f %15f %15f %15f ',h(i),dg_plus(i),dg(x)-dg_plus(i),(dg(x)-dg_plus(i))/h(i),(dg(x)-dg_plus(i))/h(i)^2)
   end
   disp('______________________________________________________________________________')
    disp('h                 D0g            dg(0)-D0g        (dg(0)-D0g)/h       (dg(0)-D0g)/h^2')
    disp('_______________________________________________________________________________')
    for i=1:n
       fprintf('%f %15f %15f %15f %15f ',h(i),dg_plus(i),dg(x)-dg_zero(i),(dg(x)-dg_zero(i))/h(i),(dg(x)-dg_zero(i))/h(i)^2)
   end

______________________________________________________________________________
h                  D+g            dg(0)-D+g        (dg(0)-D+g)/h       (dg(0)-D+g)/h^2
______________________________________________________________________________
0.500000           0.958851           0.041149           0.082298            0.164596
0.250000           0.989616           0.010384           0.041537            0.166147
0.125000           0.997398           0.002602           0.020817            0.166537
0.062500           0.999349           0.000651           0.010415            0.166634
0.031250           0.999837           0.000163           0.005208            0.166659
______________________________________________________________________________
h                 D0g            dg(0)-D0g        (dg(0)-D0g)/h       (dg(0)-D0g)/h^2
_______________________________________________________________________________
0.500000           0.958851           0.041149           0.082298            0.164596
0.250000           0.989616           0.010384           0.041537            0.166147
0.125000           0.997398           0.002602           0.020817            0.166537
0.062500           0.999349           0.000651           0.010415            0.166634
0.031250           0.999837           0.000163           0.005208            0.166659