Experiment: 1 Taylor Series
Function File:
function [taylor_approx]=taylor_series(func, degree, point, x)
taylor_approx = 0;
for n = 0:degree
nth_derivative = diff(func, n);
nth_derivative_value = double(subs(nth_derivative,
point));
nth_term = (nth_derivative_value / factorial(n)) * (x -
point)^n;
taylor_approx = taylor_approx + nth_term;
end
end
Problem: 1
syms x;
func = sin(x);
degree = 3;
point = 0;
taylor_approx = taylor_series(func, degree, point, x);
figure;
fplot(func, [-pi, pi], 'LineWidth', 2);
hold on;
fplot(taylor_approx, [-pi, pi], '--r', 'LineWidth', 1.5);
legend('Original Function', 'Taylor Series Approximation');
xlabel('x');
ylabel('y');
title('Taylor Series Approximation of sin(x)');
grid on;
zoom on;
hold off;
Problem: 2
syms x;
func = sin(x);
point = 0;
taylor_approx1 = taylor_series(func, 6, point, x);
taylor_approx2 = taylor_series(func, 8, point, x);
taylor_approx3 = taylor_series(func, 10, point, x);
figure;
fplot(func, [-pi, pi], 'LineWidth', 2);
hold on;
fplot(taylor_approx1, [-pi, pi], '--g', 'LineWidth', 1.5);
fplot(taylor_approx2, [-pi, pi], '--r', 'LineWidth', 1.5);
fplot(taylor_approx3, [-pi, pi], '--b', 'LineWidth', 1.5);
legend('Original Function', 'Taylor Series Approximation');
xlabel('x');
ylabel('y');
title('Taylor Series Approximation of sin(x)');
grid on;
zoom on;
hold off;
Problem: 3
syms x;
func = cos(x);
degree = 8;
point = 0;
taylor_approx = taylor_series(func, degree, point, x);
figure;
fplot(func, [-pi, pi], 'LineWidth', 2);
hold on;
fplot(taylor_approx, [-pi, pi], '--r', 'LineWidth', 1.5);
legend('Original Function', 'Taylor Series Approximation');
xlabel('x');
ylabel('y');
title('Taylor Series Approximation of cos(x)');
grid on;
zoom on;
hold off;
Problem: 4
syms x;
func = 1/(1-x);
degree = 10;
point = 0;
taylor_approx = taylor_series(func, degree, point, x);
figure;
fplot(func, [-pi, pi], 'LineWidth', 2);
hold on;
fplot(taylor_approx, [-pi, pi], '--r', 'LineWidth', 1.5);
legend('Original Function', 'Taylor Series Approximation');
xlabel('x');
ylabel('y');
title('Taylor Series Approximation of 1/(1-x)');
grid on;
zoom on;
hold off;
Problem: 5
syms x;
func = 1/(x);
degree = 4;
point = -1;
taylor_approx = taylor_series(func, degree, point, x);
figure;
fplot(func, [-pi, pi], 'LineWidth', 2);
hold on;
fplot(taylor_approx, [-pi, pi], '--r', 'LineWidth', 1.5);
legend('Original Function', 'Taylor Series Approximation');
xlabel('x');
ylabel('y');
title('Taylor Series Approximation of 1/(x)');
grid on;
zoom on;
hold off;
Problem: 6
syms x;
func = (-0.1*x^4-0.15*x^3-0.5*x^2-.25*x+1.2);
degree = 4;
point = 0;
taylor_approx = taylor_series(func, degree, point, 1);
figure;
fplot(func, [-pi, pi], 'LineWidth', 2);
hold on;
fplot(taylor_approx, [-pi, pi], '--r', 'LineWidth', 1.5);
legend('Original Function', 'Taylor Series Approximation');
xlabel('x');
ylabel('y');
title('Taylor Series Approximation of
(-0.1*x^4-0.15*x^3-0.5*x^2-.25*x+1.2)');
grid on;
zoom on;
hold off;
Problem: 7
syms x;
func = cos(x);
point = pi/4;
degree = 7;
h = pi/12;
taylor_approx = taylor_series(func, degree, point, x);
figure;
hold on;
fplot(func, [pi/4, pi/3], 'LineWidth', 2);
fplot(taylor_approx, [pi/4, pi/3], '--r', 'LineWidth', 1.5);
legend('Original Function', 'Taylor Series Approximation of
cos(x)');
xlabel('x');
ylabel('y');
title('Taylor Series Approximation');
grid on;
zoom on;
hold off;
Experiment: 2 Different Methods
Topic: Bisection Method
Function File:
function [root, x1, x2, rootValue, error] =
bisection_method(func, x1, x2, error_limit, max_iter)
if func(x1) * func(x2) > 0
error('cannot proceed.');
end
iter = 0;
root = 0;
error = 1;
while error > error_limit && iter < max_iter
iter = iter + 1;
root = (x1 + x2) / 2;
rootValue = func(root);
if func(x1) * rootValue < 0
x2 = root;
else
x1 = root;
end
error = abs(x2 - x1) / 2;
x1 = x1;
x2 = x2;
end
end
Problem: 1
func = @(x) (sin(10*x) + cos(3*x));
xl = 12;
xu = 16;
err_limit = 0.5;
max_iter = 50;
true_value = 12.566;
[iteration, xl, xu, rootValue, err] = bisection_method(func,
xl, xu, err_limit, max_iter);
true_error = abs((true_value - rootValue) / true_value) * 100;
ResultTable = table(iteration', xl', xu', rootValue', err',
true_error', ...
'VariableNames', {'Iteration', 'xl', 'xu', 'xr', 'Ea',
'Et'});
disp(ResultTable);
plot(iteration, true_error, '-o', 'LineWidth', 2);
xlabel('Iteration');
ylabel('true_error');
grid on;
hold on;
zoom on;
Topic: False Position Method
Function File:
function [iteration, XL, XU, rootValue, err] =
false_position(func, xl, xu, err_limit, max_iter)
if sign(func(xl)) == sign(func(xu))
error('cannot proceed');
end
iter = 1;
iteration(1) = 0;
rootValue(1) = 0;
err(1) = 100;
root = 0;
XL(1) = xl;
XU(1) = xu;
while err(iter) > err_limit && iter < max_iter
xr = xu - (func(xu) * (xl - xu)) / (func(xl) - func(xu));
fr = func(xr);
if sign(func(xl)) * sign(fr) < 0
xu = xr;
else
xl = xr;
end
iter = iter + 1;
rootValue(iter) = xr;
err(iter) = abs((xr - root) / xr) * 100;
iteration(iter) = iter;
XL(iter) = xl;
XU(iter) = xu;
root = xr;
end
end
func = @(x) sin(10*x) + cos(3*x);
xl = 12;
xu = 16;
err_limit = 0.5; max_iter = 100;
[iteration, XL, XU, rootValue, err] = false_position(func, xl, xu, err_limit,
max_iter);
disp('Iteration | XL | XU | Root Value | Error (%)');
disp('-------------------------------------------------------');
for i = 1:length(iteration)
disp([iteration(i), XL(i), XU(i), rootValue(i), err(i)]);
end
x = linspace(12, 16, 500);
y = func(x);
figure;
plot(x, y, 'b', 'LineWidth', 2);
hold on;
function[iteration,XL,XU,rootValue,err]=secant_method(func,x0,x1,err_limit,ma
x_iter)
iter=1;
iteration(1)=0;
rootValue(1)=0;
err(1)=100;
XL(1)=x0;
XU(1)=x1;
while err(iter) > err_limit && iter < max_iter
f0 = func(x0);
f1 = func(x1);
x_new = x1 - f1 * (x1 - x0) / (f1 - f0);
iter = iter + 1;
rootValue(iter) = x_new;
iteration(iter) = iter;
err(iter) = abs((x_new - x1) / x_new) * 100;
XL(iter) = x0;
XU(iter) = x1;
x0 = x1;
x1 = x_new;
end
end
func = @(x) exp(-x) - x;
x_true = 0.56714329;
x0 = 0.5;
x1 = 0.6;
err_limit = 0.5;
max_iter = 100;
iter = 1;
iteration(1) = 0;
rootValue(1) = 0;
err(1) = 100;
XL(1) = x0;
XU(1) = x1;
while err(iter) > err_limit && iter < max_iter
f0 = func(x0);
f1 = func(x1);
x_new = x1 - f1 * (x1 - x0) / (f1 - f0);
iter = iter + 1;
rootValue(iter) = x_new;
iteration(iter) = iter;
err(iter) = abs((x_new - x_true) / x_true) * 100;
XL(iter) = x0;
XU(iter) = x1;
x0 = x1;
x1 = x_new;
end
disp('Iteration | XL | XU | Root Value | Error (%)');
disp('-------------------------------------------------------');
for i = 1:length(iteration)
disp([iteration(i), XL(i), XU(i), rootValue(i), err(i)]);
end
function [iteration, rootValue, err] = newton_raphson(func, x0, err_limit,
max_iter)
iter = 1;
iteration(1) = 0;
rootValue(1) = 0;
err(1) = 100;
dfdx=diff(func);
x = x0;
while err(iter) > err_limit && iter < max_iter
fx = func(x);
dfx = dfdx(x);
x_new = x - fx / dfx;
iter = iter + 1;
rootValue(iter) = x_new;
iteration(iter) = iter;
err(iter) = abs((x_new - x) / x_new) * 100;
x = x_new;
end
end
f = @(x,y) -2 * x.^3 + 12 * x.^2 - 20 * x + 8.5;
exact_eqn = @(x,y) 0.5 * x.^4 + 4 * x.^3 - 10 * x.^2 + 8.5 * x + 1;
f1 = @(x,y,h) (6 * x.^2 * 24 * x - 20) / (h.^2 / 2);
f2 = @(x,y,h) (-12 * x + 24) * (h.^3 / 6);
f3 = @(x,y,h) (-12) * (h.^4 / 24);
x0 = 0;
y0 = 1;
h = 0.5;
N = 10;
x = zeros(1, N+1);
y = zeros(1, N+1);
exact_soln = zeros(1, N+1);
global_error = zeros(1, N+1);
local_error = zeros(1, N+1);
x(1) = x0;
y(1) = y0;
for i = 1:N
x(i+1) = x(i) + h;
y(i+1) = y(i) + h * f(x(i), y(i));
exact_soln(i+1) = exact_eqn(x(i+1), y(i+1));
global_error(i+1) = abs(exact_soln(i+1) - y(i+1)) / exact_soln(i+1) * 100;
local_error(i+1) = abs(f1(x(i),y(i),h) + f2(x(i),y(i),h) +
f3(x(i),y(i),h)) / exact_soln(i+1) * 100;
end
disp('X True Soln. Approx. Soln. Global Err(%) Local Err(%)');
disp([x' exact_soln' y' global_error' local_error']);
figure, plot(x, y)