Question

Design Layout References Mailings Review View Share 1) (15 points) Calculate the following for the function f(x) = e-a-x a. U
please i need the requiered MATLAB script to solve this. All calculations need to be done in matlab please
0 0
Add a comment Improve this question Transcribed image text
Answer #1

`Hey,

Note: Brother in case of any queries, just comment in box I would be very happy to assist all your queries

clc
%%Matlab code for forward backward and central difference method
clear all
close all

%function for which derivative have to find
f=@(x) exp(-2*x)-x;

%Function for exact derivative of function
df=@(x) (-2*exp(-2*x))-1;

%exact solution for f'(x) at x=2
ext_val=df(2);

%x value at which derivative have to find
x0=2;

%all h values
hh=linspace(0.5,0.01,40);
%loop for forward backward and central difference formulae
fprintf('\th,\tcntrl_diff,\tfrwd_diff,\tbkwd_diff\n')
for i=1:length(hh)
%all step size
h=hh(i);
%central diff
cn_frwd(i)=(f(x0+h)-f(x0-h))/(2*h);
  
%forward diff
fd_frwd(i)=(-f(x0+2*h)+4*f(x0+h)-3*f(x0))/(2*h);
%backward diff
bk_frwd(i)=(3*f(x0)-4*f(x0-h)+f(x0-2*h))/(2*h);
%exaxt differential
ext_diff(i)=ext_val;
  
%printing the result
fprintf('\t%.2f, \t%.4f, \t %.4f, \t %.4f.\n',h,cn_frwd(i),fd_frwd(i),bk_frwd(i))
end
%Loglog plot of true error vs h
plot(hh,cn_frwd,'linewidth',2)
hold on
plot(hh,fd_frwd,'linewidth',2)
plot(hh,bk_frwd,'linewidth',2)
plot(hh,ext_diff,'linewidth',2)

xlabel('step size (h)')
ylabel('numerical differential')
title('numerical differential vs. step size plot')
legend('Central difference','Forward difference','Backward difference','exact differential')

MATLAB R2018a Figure ② ▼| rch Documentation Log In EDITOR nsert Comment Indent Eile Edit Yiew Insert Iools Desktop Window Hel

Kindly revert for any queries

Thanks.

Add a comment
Know the answer?
Add Answer to:
please i need the requiered MATLAB script to solve this. All calculations need to be done in matlab please Design Layout References Mailings Review View Share 1) (15 points) Calculate the followi...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT