- #1

- 880

- 0

I'd sincerely appreciate it if someone were willing to review the few lines of MATLAB code below and indicate why they don't quite yield the expected output.

## Homework Statement

I am asked to generate using MATLAB approximated values of f(x)=cos(x) at nodes x+h,x-h with random errors <=5*10

^{-6}(using

*rand*) for h=10

^{-8},10

^{-7},...,10

^{-1}. Hence,

f

^{~}(x+h)=f(x+h)+e(x+h)

f

^{~}(x-h)=f(x-h)+e(x-h)

where |e(x)|<=5*10

^{-6}

in order to then find approximation for f'(1.2) by using the approximation:

f'(x)=[f(x+h)-f(x-h)]/2h

I am finally asked to plot the error with respect to the value of h.

Below is my code. I am not really sure why it yields one line across the y axis and another across the x axis.

## Homework Equations

## The Attempt at a Solution

*h=(10^-1).^[1:8];*

x=1.2;

fminush=cos(x-h)+(5e-6)*rand(1,1);

fplush=cos(x+h)+(5e-6)*rand(1,1);

fder=(fplush-fminush)./(2*h);

plot(h,abs(-sin(x)-fder))

x=1.2;

fminush=cos(x-h)+(5e-6)*rand(1,1);

fplush=cos(x+h)+(5e-6)*rand(1,1);

fder=(fplush-fminush)./(2*h);

plot(h,abs(-sin(x)-fder))