0% found this document useful (0 votes)
14 views

Numerics Assignment

Uploaded by

mulukenuni
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Numerics Assignment

Uploaded by

mulukenuni
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Question 1.

% The range for x

x = (1:1:10);

% 1. Plot y = 2x.^3 - 4

y = 2*x.^3 - 4;

figure;

plot(x, y, 'g*:');

xlabel('x');

ylabel('y');

subtitle('Plot of y = 2x.^3 - 4');

grid on;

% Plot z = x + 1

z = x + 1;

figure;

plot(x, z, 'g*:');

xlabel('x');

ylabel('y');
title('Plot of z = x + 1');

grid on;

% Plot w = 2 - sqrt(x)

w = 2 - sqrt(x);

figure;

plot(x, w, 'g*:');

xlabel('x');

ylabel('y');

title('Plot of w = 2 - sqrt(x)');

grid on;
% Plot v = x.^2 + 3

v = x.^2 + 3;

figure;

plot(x, v, 'g*:');

xlabel('x');

ylabel('y');

title('Plot of v = x.^2 + 3');

grid on;

Question 2.

M = [0.1 0.2 0.5 0.7 0.3;0.2 0.8 0.5 0.6 0.3;0.5 0.9 0.9 0.4 0.4;0.4 0.4 0.5 0.7 0.9;0.1 0.3 0.4 0.6 0.8];

% i.Use the mesh command

figure;

mesh(M);
% ii.Use the surf command

figure;

surf(M);

% iii.Use the contour command

figure;

contour(M);

% iv.Use the surfc command

figure;

surfc(M);
Question 3.

% range of x values

x = 1:9;

% Calculate the y values using the function

y = x.^3 - 2;

% Plot the graph

plot(x, y, 'b*:');

xlabel('x');

ylabel('y');

subtitle('Plot of y = x.^3 - 2');

grid on;

Question 4.

% Given data

x = [0.9, 1.5, 3, 4, 6, 8, 9.5];

y = [0.9, 1.5, 2.5, 5.1, 4.5, 4.9, 6.3];

% Perform polynomial curve fitting

degree = 3; % You can adjust the degree of the polynomial fit

p = polyfit(x, y, degree);

% Generate a finer x range for plotting

x_range = linspace(min(x), max(x), 100);


% Evaluate the polynomial at the finer x range

y_fit = polyval(p, x_range);

% Plot the original data and the fitted curve

plot(x, y, 'bo', x_range, y_fit, 'black-');

subtitle('Polynomial Curve Fitting');

xlabel('x');

ylabel('y');

legend('Original Data', 'Fitted Curve', 'Location', 'best');

grid on;

Question 5.

A = [2 -4 -1 3 -1;1 1 -2 0 1;-1 -3 0 1 3;3 -1 -1 4 -1;1 1 -1 2 0];

B = [3; 6; -4; 1; 5];

% Solve the linear system Ax = B using backslash operator

c = A\B;

% Display the solution

disp('The roots are:');

disp(['x1 = ', num2str(c(1))]);

disp(['x2 = ', num2str(c(2))]);

disp(['x3 = ', num2str(c(3))]);

disp(['x4 = ', num2str(c(4))]);


disp(['x5 = ', num2str(c(5))]);

Question 6.

% Plot the function

x = [0:0.5:4];

y = x.^3 - 6*x.^2 + 11*x - 6.1;

plot(x, y,'b*:');

xlabel('x')

ylabel('f(x)')

suptitle('Graph of x^3 - 6x^2 + 11x - 6.1');

grid on

% Define the function and its derivative

f = @(x) x.^3 - 6*x.^2 + 11*x - 6.1;

df = @(x) 3*x.^2 - 12*x + 11;

% Initial guess

x0 = 3.5;

% Newton-Raphson method (3 iterations)

disp('Iteration 1:');

x1 = x0 - f(x0)/df(x0);

disp(['x1 = ', num2str(x1)]);


disp('Iteration 2:');

x2 = x1 - f(x1)/df(x1);

disp(['x2 = ', num2str(x2)]);

disp('Iteration 3:');

x3 = x2 - f(x2)/df(x2);

disp(['x3 = ', num2str(x3)]);

% Display the final result

disp('The roots of the equation are:');

disp(['The highest real root is: ', num2str(x3)]);

% Define the function

f = @(x) x.^3 - 6*x.^2 + 11*x - 6.1;

% Initial guesses

xi = 2.5;

disp(['xi = ', num2str(xi)]);

x1 = 3.5;

disp(['x1 = ', num2str(x1)]);

% Secant method (3 iterations)

disp('Iteration 1:');

x2 = x1 - f(x1)*(x1 - xi)/(f(x1) - f(xi));

disp(['x2 = ', num2str(x2)]);

disp('Iteration 2:');

x3 = x2 - f(x2)*(x2 - x1)/(f(x2) - f(x1));

disp(['x3 = ', num2str(x3)]);

disp('Iteration 3:');
x4 = x3 - f(x3)*(x3 - x2)/(f(x3) - f(x2));

disp(['x4 = ', num2str(x4)]);

% Display the final result

disp('The roots of the equation are:');

disp(['The highest real root is: ', num2str(x4)]);

% Define the function and its derivative

f = @(x) x.^3 - 6*x.^2 + 11*x - 6.1;

df = @(x) 3*x.^2 - 12*x + 11;

% Initial guess and delta

x0 = 3.5;

delta = 0.01;

% Modified secant method (3 iterations)

disp('Iteration 1:');

x1 = x0 - f(x0)/df(x0 + delta);

disp(['x1 = ', num2str(x1)]);

disp('Iteration 2:');

x2 = x1 - f(x1)/df(x1 + delta);

disp(['x2 = ', num2str(x2)]);

disp('Iteration 3:');

x3 = x2 - f(x2)/df(x2 + delta);

disp(['x3 = ', num2str(x3)]);

% Display the final result

disp('The roots of the equation are:');

disp(['The highest real root is: ', num2str(x3)]);


coefficients = [1 -6 11 -6.1];

roots(coefficients)

Question 7.

A = [0 2 5; 2 1 1; 3 1 0];

b = [1; 1; 2];

% (a) Compute the determinant

det_A = det(A);

disp('The determinant of the coefficient matrix is:');

disp(det_A);

% (b) Use Cramer's rule to solve for the x's

x1 = det([b A(:,2:3)]) / det_A;

x2 = det([A(:,1) b A(:,3)]) / det_A;

x3 = det([A(:,1:2) b]) / det_A;

disp('The solution using Cramer''s rule:');

disp(['x1 = ', num2str(x1)]);

disp(['x2 = ', num2str(x2)]);

disp(['x3 = ', num2str(x3)]);

% (c) Use Gauss elimination with partial pivoting to solve for the x's

[L, U, P] = lu(A);

y = P * b;

x = U \ (L \ y);

disp('The solution using Gauss elimination with partial pivoting:');

disp(['x1 = ', num2str(x(1))]);

disp(['x2 = ', num2str(x(2))]);


disp(['x3 = ', num2str(x(3))]);

% Verify the determinant value

det_A_new = det(A);

disp('The determinant of the coefficient matrix (using Gauss elimination):');

disp(det_A_new);

% (d) Substitute the results back into the original equations

eq1 = 2 * x(2) + 5 * x(3);

eq2 = 2 * x(1) + x(2) + x(3);

eq3 = 3 * x(1) + x(2);

disp('Verification of the solution:');

disp(['Equation 1: ', num2str(eq1)]);

disp(['Equation 2: ', num2str(eq2)]);

disp(['Equation 3: ', num2str(eq3)]);

Question 8.

c=[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

A=[ 0, 1, 1, 0, 0, 0, 0, 0, 0, 0; % Ax + AD = 0

1, 0, 0, 1, 0, 0, 0, 0, 0, 0; % Ay + AB = 0

0, 0, 0, 0, 1, 3/5, 0, 0, 0, 0; % 74 + BC + 3/5 BD = 0

-1, 0, 0, 0, 0, -4/5, 0, 0, 0, 0; % -AB - 4/5 BD = 0

0, 0, 0, 0, -1, 0, 0, 3/5, 0, 0; % -BC + 3/5 CE = 0

0, 0, 0, 0, 0, 0, -1, -4/5, 0, 0; % -24 - CD - 4/5 CE = 0

0, 0, -1, 0, 0, -3/5, 0, 0, 1, 0; % -AD + DE - 3/5 BD = 0

0, 0, 0, 0, 0, 4/5, 1, 0, 0, 0; % CD + 4/5 BD = 0

0, 0, 0, 0, 0, 0, 0, -3/5, -1, 0; % -DE - 3/5 CE = 0

0, 0, 0, 0, 0, 0, 0, 4/5, 0, 1; % Ey + 4/5 CE = 0
];

b=[

0; % Ax + AD = 0

0; % Ay + AB = 0

-74; % 74 + BC + 3/5 BD = 0

0; % -AB - 4/5 BD = 0

0; % -BC + 3/5 CE = 0

-24; % -24 - CD - 4/5 CE = 0

0; % -AD + DE - 3/5 BD = 0

0; % CD + 4/5 BD = 0

0; % -DE - 3/5 CE = 0

0; % Ey + 4/5 CE = 0

];

% Solve the system of equations

x = A \ b;

% Display the results

AB = x(1);

AD = x(2);

Ax = x(3);

Ay = x(4);

BC = x(5);

BD = x(6);

CD = x(7);

CE = x(8);
DE = x(9);

Ey = x(10);

fprintf('Ax = %.2f\n', Ax);

fprintf('Ay = %.2f\n', Ay);

fprintf('AD = %.2f\n', AD);

fprintf('AB = %.2f\n', AB);

fprintf('BC = %.2f\n', BC);

fprintf('BD = %.2f\n', BD);

fprintf('CD = %.2f\n', CD);

fprintf('CE = %.2f\n', CE);

fprintf('DE = %.2f\n', DE);

fprintf('Ey = %.2f\n', Ey);

Question 9.

A = [0.52 0.2 0.25;0.3 0.5 0.3;0.18 0.3 0.55];

B = [4800;5800;5700];

x = A\B;

disp('The values are:');

disp(['Pit 1: ' num2str(x(1))]);

disp(['Pit 2: ' num2str(x(2))]);

disp(['Pit 3: ' num2str(x(3))]);


UNIVERSITY OF GONDAR
INSTITUTE OF TECHNOLOGY

DEPARTMENT OF MECHANICAL ENGINEERING

NUMERICAL METHODS

Fasika Bekele
Id Number: 40879/13

Submitted To: Lecturer Gizachew D.

You might also like