0% found this document useful (0 votes)
9 views6 pages

4506 Lab6assignment

The document contains MATLAB code that defines symbolic functions and computes their derivatives using both analytical and numerical methods, including central, forward, and backward differences. It generates plots to compare the analytical derivatives with the numerical approximations for two different functions over specified intervals. The results are visualized with legends and labels for clarity.

Uploaded by

fabihaanbar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views6 pages

4506 Lab6assignment

The document contains MATLAB code that defines symbolic functions and computes their derivatives using both analytical and numerical methods, including central, forward, and backward differences. It generates plots to compare the analytical derivatives with the numerical approximations for two different functions over specified intervals. The results are visualized with legends and labels for clarity.

Uploaded by

fabihaanbar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

1.

syms x
my_func(x) = exp(-x)*sin(3*x); % Define the symbolic function

arr = linspace(0, 5, 100); % Create an array of 100 points between 0 and 5

% Evaluate the function at the points in 'arr'


func_values = double(my_func(arr));

diff_func = diff(my_func, x);

% Evaluate the analytical derivative at the points in 'arr'


diff_values = double(diff_func(arr))

diff_values = 1×100
3.0000 2.6761 2.3185 1.9391 1.5492 1.1593 0.7788 0.4160

h = arr(2) - arr(1); %step size

% Central difference method


xCentral = arr(2:end-1);
dFCentral = (func_values(3:end) - func_values(1:end-2)) / (2*h);

% Forward difference method


xForward = arr(1:end-1);
dFForward = (func_values(2:end) - func_values(1:end-1)) / h;

% Backward difference method


xBackward = arr(2:end);
dFBackward = (func_values(2:end) - func_values(1:end-1)) / h;

% Plot the results


plot(arr, diff_values , 'b', 'LineWidth', 2); % Analytical derivative
hold on;
plot(xCentral, dFCentral, 'r', 'LineWidth', 2); % Central difference
plot(xForward, dFForward, 'k', 'LineWidth', 2); % Forward difference
plot(xBackward, dFBackward, 'g', 'LineWidth', 2); % Backward difference

% Add legend and labels


legend('Analytic', 'Central', 'Forward', 'Backward');
xlabel('x');
ylabel('Derivative');
title('Comparison of Analytical and Numerical Derivatives');
grid on;
hold off;

1
2.
syms x
my_func(x) = sin(3*x)+exp(-x)+3*cos(x); % Define the symbolic function

arr = linspace(0, 5, 100); % Create an array of 100 points between 0 and 5

% Evaluate the function at the points in 'arr'


func_values = double(my_func(arr));

diff_func = diff(my_func, x);

% Evaluate the analytical derivative at the points in 'arr'


diff_values = double(diff_func(arr))

diff_values = 1×100
2.0000 1.8634 1.6569 1.3832 1.0467 0.6531 0.2096 -0.2758

h = arr(2) - arr(1);

% Central difference method


xCentral = arr(2:end-1);
dFCentral = (func_values(3:end) - func_values(1:end-2)) / (2*h);

2
% Forward difference method
xForward = arr(1:end-1);
dFForward = (func_values(2:end) - func_values(1:end-1)) / h;

% Backward difference method


xBackward = arr(2:end);
dFBackward = (func_values(2:end) - func_values(1:end-1)) / h;

% Plot the results


plot(arr, diff_values,'b', 'LineWidth', 2); % Analytical derivative
hold on;
plot(xCentral, dFCentral, 'r', 'LineWidth', 2); % Central difference
plot(xForward, dFForward, 'k', 'LineWidth', 2); % Forward difference
plot(xBackward, dFBackward, 'g', 'LineWidth', 2); % Backward difference

% Add legend and labels


legend('Analytic', 'Central', 'Forward', 'Backward');
xlabel('x');
ylabel('Derivative');
title('Comparison of Analytical and Numerical Derivatives');
grid on;
hold off;

3.

3
syms x
my_func(x) = exp(-x)*sin(3*x); % Define the symbolic function

arr = linspace(0, 5, 1000); % Create an array of 100 points between 0 and 5

% Evaluate the function at the points in 'arr'


func_values = double(my_func(arr))

func_values = 1×1000
0 0.0149 0.0297 0.0444 0.0588 0.0732 0.0873 0.1013

diff_func = diff(my_func, x);

% Evaluate the analytical derivative at the points in 'arr'


diff_values = double(diff_func(arr))

diff_values = 1×1000
3.0000 2.9697 2.9391 2.9079 2.8764 2.8445 2.8121 2.7794

h = arr(2) - arr(1);
% Central difference method
xCentral = arr(2:end-1);
dFCentral = (func_values(3:end) - func_values(1:end-2)) / (2*h);
% Forward difference method
xForward = arr(1:end-1);
dFForward = (func_values(2:end) - func_values(1:end-1)) / h;
% Backward difference method
xBackward = arr(2:end);
dFBackward = (func_values(2:end) - func_values(1:end-1)) / h;
% Plot the results
plot(arr, diff_values , 'b', 'LineWidth', 2); % Analytical derivative
hold on;
plot(xCentral, dFCentral, 'r', 'LineWidth', 2); % Central difference
plot(xForward, dFForward, 'k', 'LineWidth', 2); % Forward difference
plot(xBackward, dFBackward, 'g', 'LineWidth', 2); % Backward difference

% Add legend and labels


legend('Analytic', 'Central', 'Forward', 'Backward');
xlabel('x');
ylabel('Derivative');
title('Comparison of Analytical and Numerical Derivatives');
grid on;
hold off;

4
syms x
my_func(x) = sin(3*x)+exp(-x)+3*cos(x); % Define the symbolic function
arr = linspace(0, 5, 2000); % Create an array of 2000 points between 0 and 5
% Evaluate the function at the points in 'arr'
func_values = double(my_func(arr));
diff_func = diff(my_func, x);

% Evaluate the analytical derivative at the points in 'arr'


diff_values = double(diff_func(arr))

diff_values = 1×2000
2.0000 1.9949 1.9896 1.9842 1.9786 1.9728 1.9668 1.9607

h = arr(2) - arr(1);
% Central difference method
xCentral = arr(2:end-1);
dFCentral = (func_values(3:end) - func_values(1:end-2)) / (2*h);
% Forward difference method
xForward = arr(1:end-1);
dFForward = (func_values(2:end) - func_values(1:end-1)) / h;
% Backward difference method
xBackward = arr(2:end);
dFBackward = (func_values(2:end) - func_values(1:end-1)) / h;
% Plot the results
plot(arr, diff_values,'b', 'LineWidth', 2); % Analytical derivative
hold on;

5
plot(xCentral, dFCentral, 'r', 'LineWidth', 2); % Central difference
plot(xForward, dFForward, 'k', 'LineWidth', 2); % Forward difference
plot(xBackward, dFBackward, 'g', 'LineWidth', 2); % Backward difference

% Add legend and labels


legend('Analytic', 'Central', 'Forward', 'Backward');
xlabel('x');
ylabel('Derivative');
title('Comparison of Analytical and Numerical Derivatives');
grid on;
hold off;

You might also like