Matlab simple loop for different function variables (Finite Difference)

35 ビュー (過去 30 日間)
cagri aydin
cagri aydin 2017 年 3 月 8 日
コメント済み: Rahul Kalampattel 2017 年 3 月 8 日
So, i wrote a simple matlab script to evaluate forward, backward and central difference approximations of first and second derivatives for a spesific function (y = x^3-5x) at two different x values (x=0.5 and x = 1.5) and for 7 different step sizes (h) and compare the relative errors of the approximations to the analytical derivatives.
However, i need to enter x and h values manually every time. Question is, how do i create a loop for 7 different h values and 2 different x values and get all the results as a matrix?
Script:
clc
clear all
close all
h = 0.00001; %step size
x1 = 0.5; %x value
y = @(x) x.^3 - 5*x; %main function
dy = @(x) 3*x.^2 - 5; %first derivative
ddy = @(x) 6*x; %second derivative
d1 = dy(x1);
d2 = ddy(x1);
%Forward Differencing
f1 = (y(x1+h) - y(x1))/h;
f2 = (y(x1+2*h) - 2*y(x1+h) + y(x1))/(h.^2);
%Central Differencing
c1 = (y(x1+h)-y(x1-h))/(2*h);
c2 = (y(x1+h)-2*y(x1)+y(x1-h))/(h.^2);
% Backward Differencing
b1 = (y(x1) - y(x1-h))/h;
b2 = (y(x1)-2*y(x1-h)+y(x1-2*h))/(h.^2);
% Relative Errors
ForwardError1 = (f1 - dy(x1))/dy(x1);
ForwardError2 = (f2 - ddy(x1))/ddy(x1);
CentralError1 = (c1 - dy(x1))/dy(x1);
CentralError2 = (c2 - ddy(x1))/ddy(x1);
BackwardError1 = (b1 - dy(x1))/dy(x1);
BackwardError2 = (b2 - ddy(x1))/ddy(x1);

採用された回答

Rahul Kalampattel
Rahul Kalampattel 2017 年 3 月 8 日
Something like this? Or did you want all the error terms in one 3D matrix?
clc
clearvars
close all
hVec = [0.00001 0.00005 0.0001 0.0005 0.001 0.005 0.01]; %step size
x1Vec = [0.5 1.5]; %x value
y = @(x) x.^3 - 5*x; %main function
dy = @(x) 3*x.^2 - 5; %first derivative
ddy = @(x) 6*x; %second derivative
for i = 1:2
for j=1:7
x1 = x1Vec(i);
h = hVec(j);
d1 = dy(x1);
d2 = ddy(x1);
%Forward Differencing
f1 = (y(x1+h) - y(x1))/h;
f2 = (y(x1+2*h) - 2*y(x1+h) + y(x1))/(h.^2);
%Central Differencing
c1 = (y(x1+h)-y(x1-h))/(2*h);
c2 = (y(x1+h)-2*y(x1)+y(x1-h))/(h.^2);
% Backward Differencing
b1 = (y(x1) - y(x1-h))/h;
b2 = (y(x1)-2*y(x1-h)+y(x1-2*h))/(h.^2);
% Relative Errors
ForwardError1(i,j) = (f1 - dy(x1))/dy(x1);
ForwardError2(i,j) = (f2 - ddy(x1))/ddy(x1);
CentralError1(i,j) = (c1 - dy(x1))/dy(x1);
CentralError2(i,j) = (c2 - ddy(x1))/ddy(x1);
BackwardError1(i,j) = (b1 - dy(x1))/dy(x1);
BackwardError2(i,j) = (b2 - ddy(x1))/ddy(x1);
end
end
  2 件のコメント
cagri aydin
cagri aydin 2017 年 3 月 8 日
Thank you, this is quite enough of an answer for me. Although, if possible, it would be very helpful to learn how to get all the error terms in one 3D matrix.
Rahul Kalampattel
Rahul Kalampattel 2017 年 3 月 8 日
Just change the last bit to
% Relative Errors
RelativeError(i,j,1) = (f1 - dy(x1))/dy(x1); % Forward, first
RelativeError(i,j,2) = (f2 - ddy(x1))/ddy(x1); % Forward, second
RelativeError(i,j,3) = (c1 - dy(x1))/dy(x1); % Central, first
RelativeError(i,j,4) = (c2 - ddy(x1))/ddy(x1); % Central, second
RelativeError(i,j,5) = (b1 - dy(x1))/dy(x1); % Backward, first
RelativeError(i,j,6) = (b2 - ddy(x1))/ddy(x1); % Backward, second
RelativeError will be a 2x7x6 matrix. Each of the 6 layers corresponds to a different error, and within a layer the rows correspond to x and the columns correspond to h.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by