Numerics Assignment
Numerics Assignment
x = (1:1:10);
% 1. Plot y = 2x.^3 - 4
y = 2*x.^3 - 4;
figure;
plot(x, y, 'g*:');
xlabel('x');
ylabel('y');
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');
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];
figure;
mesh(M);
% ii.Use the surf command
figure;
surf(M);
figure;
contour(M);
figure;
surfc(M);
Question 3.
% range of x values
x = 1:9;
y = x.^3 - 2;
plot(x, y, 'b*:');
xlabel('x');
ylabel('y');
grid on;
Question 4.
% Given data
p = polyfit(x, y, degree);
xlabel('x');
ylabel('y');
grid on;
Question 5.
c = A\B;
Question 6.
x = [0:0.5:4];
plot(x, y,'b*:');
xlabel('x')
ylabel('f(x)')
grid on
% Initial guess
x0 = 3.5;
disp('Iteration 1:');
x1 = x0 - f(x0)/df(x0);
x2 = x1 - f(x1)/df(x1);
disp('Iteration 3:');
x3 = x2 - f(x2)/df(x2);
% Initial guesses
xi = 2.5;
x1 = 3.5;
disp('Iteration 1:');
disp('Iteration 2:');
disp('Iteration 3:');
x4 = x3 - f(x3)*(x3 - x2)/(f(x3) - f(x2));
x0 = 3.5;
delta = 0.01;
disp('Iteration 1:');
x1 = x0 - f(x0)/df(x0 + delta);
disp('Iteration 2:');
x2 = x1 - f(x1)/df(x1 + delta);
disp('Iteration 3:');
x3 = x2 - f(x2)/df(x2 + delta);
roots(coefficients)
Question 7.
A = [0 2 5; 2 1 1; 3 1 0];
b = [1; 1; 2];
det_A = det(A);
disp(det_A);
% (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);
det_A_new = det(A);
disp(det_A_new);
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
0, 0, 0, 0, 0, 4/5, 1, 0, 0, 0; % CD + 4/5 BD = 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
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
];
x = A \ b;
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);
Question 9.
B = [4800;5800;5700];
x = A\B;
NUMERICAL METHODS
Fasika Bekele
Id Number: 40879/13