New Microsoft PowerPoint Presentation (2).pptx

kjeremiah747 8 views 20 slides Oct 25, 2025
Slide 1
Slide 1 of 20
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20

About This Presentation

The moon hung low over the ruins of Valenreach, a pale coin gleaming through tattered clouds. The wind hissed across the broken stones, whispering names no living tongue remembered. A lone traveler crossed the desolate bridge, her cloak snapping like a banner in the storm. The sigil on her shoulder ...


Slide Content

GROUP 14 ASSIGNMENT

NAME REG NO COURSE KATUMBA JEREMIAH BU/UP/2024/5252 AMI TOSKIN CHRIS BU/UP/2023/0843 WAR 3 ARACH GLADYS BU/UP/2024/1016 WAR MUTEBE JOEL BU/UP/2024/1043 WAR TWALE JOSHUA BU/UP/2024/5259 APE BASALIRWA ROBERT BU/UP/2024/1004 WAR AWENE SOLOMON BU/UP/2024/1021 WAR OMONA PATRIC BU/UP/2024/0986 MEB ACHENG AUDREY LORNA BU/UP/2024/0822 AMI ACIPA BRIGDET BU/UP/2024/1078 WAR

ASSIGNMENT QUESTION In your respective groups, utilize the knowledge of algorithm development, control structures and (overview of programming languages, introduction to MATLAB, Fundamentals of MATLAB programming and Data Visualization and plotting) from module one to four on the following problems.   a) All Numerical approximate methods for finding the solutions to functions, these include but are not limited to.   1. Newton Raphson Method   2. Secant method and Bisection Method.   b) All methods for solving differential equations numerically, these include but not limited to,   1. Range kutta 4   2. Range kutta 2   3.Euler   (c) Ensure to apply a) and b) on a practical real-world problem   d) Note: Ensure that the different methods are tested on similar problems each (minimum of two). This will help you to plot graphs that compare the problems analytical solutions to the solutions obtained by the different methods along with the computation time.  

SOLUTION clear; close all ; clc; PART 1 Root finding methods %Define first function and its derivative f = @(x) x.^3 - x - 2; % Example nonlinear function df = @(x) 3*x.^2 - 1; % Derivative of f(x)   %Parameters for iteration tol = 1e-6; % Tolerance for convergence max_iter = 100; % Maximum allowed iterations

1a) NEWTON-RAPHSON METHOD %Newton-Raphson Method fprintf( 'Running Newton-Raphson Method...\n' ); Running Newton-Raphson Method... tic; [x_nr, iter_nr] = newton_raphson(f, df, 1.5, tol, max_iter); time_nr = toc; fprintf( 'Newton-Raphson root: %.6f, iterations: %d, time: %.6f s\n' , x_nr, iter_nr, time_nr); Newton-Raphson root: 1.521380, iterations: 3, time: 0.005693 s SECANT METHOD %Secant Method fprintf( 'Running Secant Method...\n' ); Running Secant Method... tic; [x_secant, iter_secant] = secant_method(f, 1, 2, tol, max_iter); time_secant = toc; fprintf( 'Secant root: %.6f, iterations: %d, time: %.6f s\n' , x_secant, iter_secant, time_secant); Secant root: 1.521380, iterations: 7, time: 0.005751 s      

SECANT METHOD %Secant Method fprintf( 'Running Secant Method...\n' ); Running Secant Method... tic; [x_secant, iter_secant] = secant_method(f, 1, 2, tol, max_iter); time_secant = toc; fprintf( 'Secant root: %.6f, iterations: %d, time: %.6f s\n' , x_secant, iter_secant, time_secant); Secant root: 1.521380, iterations: 7, time: 0.005751 s      

1c) BISECTION METHOD %Bisection Method fprintf( 'Running Bisection Method...\n' ); Running Bisection Method... tic; [x_bisect, iter_bisect] = bisection_method(f, 1, 2, tol, max_iter); time_bisect = toc; fprintf( 'Bisection root: %.6f, iterations: %d, time: %.6f s\n' , x_bisect, iter_bisect, time_bisect); Bisection root: 1.521380, iterations: 20, time: 0.006785 s PLOT % Plot: Root-finding computation times methods = { 'Newton-Raphson' , 'Secant' , 'Bisection' }; times = [time_nr, time_secant, time_bisect];   figure; bar(times); set(gca, 'XTickLabel' , methods); ylabel( 'Computation time (seconds)' ); title( 'Root Finding Methods Computation Time' ); grid on ;

PART 2 %%Numerical Solutions of ODE % ODE: dy/dt = -2y + e^(-t) odefun = @(t,y) -2*y + exp(-t); % Analytical solution y_analytical = @(t) (1/3)*exp(-2*t) + (2/3)*exp(-t); % Initial condition and domain t0 = 0; tf = 5; y0 = 1; h = 0.1; t_vals = t0:h:tf; % Time vector N = length(t_vals); % Number of steps % Preallocate solution arrays y_euler = zeros(1, N); y_rk2 = zeros(1, N); y_rk4 = zeros(1, N); % Initial values y_euler(1) = y0; y_rk2(1) = y0; y_rk4(1) = y0;

EULER METHOD %Euler Method fprintf( '\nRunning Euler Method...\n' ); Running Euler Method... tic; for i=1:N-1 y_euler(i+1) = y_euler(i) + h*odefun(t_vals(i), y_euler(i)); end time_euler = toc; fprintf( 'Euler Method time: %.6f s\n' , time_euler); Euler Method time: 0.006484 s

RANGE KUTTA 2 %RK2 Method fprintf( 'Running RK2 Method...\n' ); Running RK2 Method... tic; for i=1:N-1 k1 = odefun(t_vals(i), y_rk2(i)); k2 = odefun(t_vals(i)+h, y_rk2(i)+h*k1); y_rk2(i+1) = y_rk2(i) + h/2*(k1 + k2); end time_rk2 = toc; fprintf( 'RK2 Method time: %.6f s\n' , time_rk2); RK2 Method time: 0.005310 s

RANGE KUTTA 4 %RK4 Method fprintf( 'Running RK4 Method...\n' ); Running RK4 Method... tic; for i=1:N-1 k1 = odefun(t_vals(i), y_rk4(i)); k2 = odefun(t_vals(i)+h/2, y_rk4(i)+h*k1/2); k3 = odefun(t_vals(i)+h/2, y_rk4(i)+h*k2/2); k4 = odefun(t_vals(i)+h, y_rk4(i)+h*k3); y_rk4(i+1) = y_rk4(i) + h/6*(k1 + 2*k2 + 2*k3 + k4); end time_rk4 = toc; fprintf( 'RK4 Method time: %.6f s\n' , time_rk4); RK4 Method time: 0.007025 s

PLOTS % Analytical solution y_true = y_analytical(t_vals); %Error Analysis err_euler = max(abs(y_true - y_euler)); err_rk2 = max(abs(y_true - y_rk2)); err_rk4 = max(abs(y_true - y_rk4)); fprintf( 'Max Errors -> Euler: %.6e, RK2: %.6e, RK4: %.6e\n' , err_euler, err_rk2, err_rk4); Max Errors -> Euler: 7.009266e-02, RK2: 8.450842e-02, RK4: 8.333152e-02 %Plot: ODE solutions figure; plot(t_vals, y_true, 'k-' , 'LineWidth' , 2); hold on ; plot(t_vals, y_euler, 'r--' ); plot(t_vals, y_rk2, 'b-.' ); plot(t_vals, y_rk4, 'g:' ); legend( 'Analytical' , 'Euler' , 'RK2' , 'RK4' , 'Location' , 'best' ); xlabel( 't' ); ylabel( 'y(t)' ); title( 'Numerical Solutions of ODE vs Analytical Solution' ); grid on ;

%Plot: ODE computation times --- figure; bar([time_euler, time_rk2, time_rk4]); set(gca, 'XTickLabel' , { 'Euler' , 'RK2' , 'RK4' }); ylabel( 'Computation time (seconds)' ); title( 'ODE Numerical Methods Computation Time' ); grid on ;

FUNCTIONS FOR ROOT FINDING %%Functions for Root Finding function [root, iter] = newton_raphson(f, df, x0, tol, max_iter) % Newton-Raphson Method for solving f(x)=0 x = x0; for iter = 1:max_iter x_new = x - f(x)/df(x); if abs(x_new - x) < tol root = x_new; return ; end x = x_new; end error( 'Newton-Raphson did not converge' ); end   function [root, iter] = secant_method(f, x0, x1, tol, max_iter) % Secant Method for solving f(x)=0 for iter = 1:max_iter f_x0 = f(x0); f_x1 = f(x1); x2 = x1 - f_x1*(x1 - x0)/(f_x1 - f_x0); if abs(x2 - x1) < tol root = x2; return ; end x0 = x1; x1 = x2; end error( 'Secant method did not converge' ); end   function [root, iter] = bisection_method(f, a, b, tol, max_iter)

% Bisection Method for solving f(x)=0 if f(a)*f(b) > 0 error( 'Function has same signs at interval endpoints' ); end for iter = 1:max_iter c = (a + b)/2; if abs(f(c)) < tol || (b - a)/2 < tol root = c; return ; end if f(a)*f(c) < 0 b = c; else a = c; end end error( 'Bisection method did not converge' ); end

PRACTICAL APPLICATION Practical applications of these numerical methods include: Population Dynamics: Logistic growth model for predicting population trends. Mechanical Systems: Root-finding for equilibrium points in structures and beams. Engineering Analysis: ODE solvers for heat transfer, fluid flow, and vibration systems.

CONLUSION This study demonstrated the effectiveness of different numerical methods for root-finding and ODE solving. The comparison highlighted trade-offs between computational efficiency and accuracy. In real-world scenarios, RK4 is recommended for ODEs due to its robustness, while Newton-Raphson is preferred for root-finding if the derivative is available. These methods remain vital tools in engineering and scientific problem-solving.