4506 Lab6assignment
4506 Lab6assignment
syms x
my_func(x) = exp(-x)*sin(3*x); % Define the symbolic function
diff_values = 1×100
3.0000 2.6761 2.3185 1.9391 1.5492 1.1593 0.7788 0.4160
1
2.
syms x
my_func(x) = sin(3*x)+exp(-x)+3*cos(x); % Define the symbolic function
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);
2
% Forward difference method
xForward = arr(1:end-1);
dFForward = (func_values(2:end) - func_values(1:end-1)) / h;
3.
3
syms x
my_func(x) = exp(-x)*sin(3*x); % Define the symbolic function
func_values = 1×1000
0 0.0149 0.0297 0.0444 0.0588 0.0732 0.0873 0.1013
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
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);
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