clc
% question 1
% -------------------------------------------------------------------------
% create a row vector v1 with 15 equally spaced elements
v1 = linspace(22, 789, 15);
% now we convert v1 to column vector v2
v2 = v1';
% displaying the vectors
disp('Row vector v1:');
disp(v1);
disp('Column vector v2:');
disp(v2);
% question 2
% % -----------------------------------------------------------------------
% first we define the vector
v = [1, 2, 3, 4, 5];
% consturcting the matrix using vector multiplication
M = v' * v;
% now we can display the resulting matrix
disp(M);
% question 3
% -------------------------------------------------------------------------
% defining the coefficient matrix A
A = [2, 2, 2;
-6, 4, -10;
4, -6, 10];
% the constant matrix B
B = [-2; 7; -6];
% solving the linear system
X = A \ B;
% now we display the solution
disp('The solution is:');
disp(X);
% question 4
% -------------------------------------------------------------------------
1
% creating the x and y data for the solid line
x = 0:15; % x values from 0 to 15
y = x; % y = x for the solid line
% now we will plot the solid line
plot(x, y, 'r-', 'LineWidth', 1.5); % the red solid line
hold on;
% potting the dashed horizontal line
y_dash = 10;
plot([0, 15], [y_dash, y_dash], 'y--', 'LineWidth', 1.5); % yellow dashed
line
% Plot the solid vertical line
x_vert = 10; % Vertical line at x = 10
plot([x_vert, x_vert], [0, 15], 'b-', 'LineWidth', 1.5); % blue solid line
% setting the axis limits
xlim([0 15]);
ylim([0 15]);
grid on;
% adding labels and title
xlabel('X-axis');
ylabel('Y-axis');
title('matlab test figure 1');
hold off;
% question 5
% (a) --------------------------------------------------------------------
f = @(x) 2*x.^3 + 6*x.^2 - 5*x + 7;
% (b) --------------------------------------------------------------------
x = linspace(-5, 5, 1000);
% evaluating f(x) for the range of x
y = f(x);
% now we will plot the function
figure;
plot(x, y, 'b-', 'LineWidth', 1.5);
hold on;
grid on;
% add a horizontal line at y = 0
plot(x, zeros(size(x)), 'k--', 'LineWidth', 1);
% labelling the axes and add a title
xlabel('x');
ylabel('f(x)');
2
title('Plot of f(x) = 2x^3 + 6x^2 - 5x + 7');
% finding and mark the zero of the function
zero_x = fzero(f, -4);
plot(zero_x, 0, 'ro', 'MarkerSize', 8, 'MarkerFaceColor', 'r');
% adding a legend
legend('f(x)', 'y = 0', 'Zero of f(x)');
disp(['The zero of the function is located at x = ', num2str(zero_x)]);
% (c) ---------------------------------------------------------------------
a = -5;
b = -3;
% checking if f(a) and f(b) have opposite signs
if f(a) * f(b) > 0
error('f(a) and f(b) must have opposite signs to use bisection.');
end
% the number of iterations
num_iterations = 10;
% computing bisection method
disp('Iteration | a | b | c | f(c)');
for i = 1:num_iterations
% finding the midpoint
c = (a + b) / 2;
fc = f(c);
% displaying the iteration values
fprintf('%4d | %8.5f | %8.5f | %8.5f | %8.5f\n', i, a, b, c, fc);
% update the interval based on the sign of f(c)
if f(a) * fc < 0
b = c;
else
a = c;
end
end
root = c;
disp(['Approximate root after ', num2str(num_iterations), ' iterations is: x
= ', num2str(root)]);
% question 5
% -------------------------------------------------------------------------
f = @(x) x.^2 .* exp(-x.^4);
% the limits of integration
3
a = -3; % lower limit
b = 3; % upper limit
% defining the number of subintervals
n = 20;
% here we calculate the width of each subinterval
h = (b - a) / n;
x = linspace(a, b, n + 1); % n+1 points for n intervals
% evaluating the function at the x points
y = f(x);
% now we apply the trapezoidal rule
I = h * (0.5 * y(1) + sum(y(2:end-1)) + 0.5 * y(end));
% displaying the result
disp(['Approximate integral using the trapezoidal rule: I = ', num2str(I)]);
Row vector v1:
Columns 1 through 7
22.0000 76.7857 131.5714 186.3571 241.1429 295.9286 350.7143
Columns 8 through 14
405.5000 460.2857 515.0714 569.8571 624.6429 679.4286 734.2143
Column 15
789.0000
Column vector v2:
22.0000
76.7857
131.5714
186.3571
241.1429
295.9286
350.7143
405.5000
460.2857
515.0714
569.8571
624.6429
679.4286
734.2143
789.0000
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
4
5 10 15 20 25
The solution is:
-0.4000
-0.1000
-0.5000
The zero of the function is located at x = -3.8775
Iteration | a | b | c | f(c)
1 | -5.00000 | -3.00000 | -4.00000 | -5.00000
2 | -4.00000 | -3.00000 | -3.50000 | 12.25000
3 | -4.00000 | -3.50000 | -3.75000 | 4.65625
4 | -4.00000 | -3.75000 | -3.87500 | 0.09766
5 | -4.00000 | -3.87500 | -3.93750 | -2.38232
6 | -3.93750 | -3.87500 | -3.90625 | -1.12531
7 | -3.90625 | -3.87500 | -3.89062 | -0.50959
8 | -3.89062 | -3.87500 | -3.88281 | -0.20491
9 | -3.88281 | -3.87500 | -3.87891 | -0.05336
10 | -3.87891 | -3.87500 | -3.87695 | 0.02221
Approximate root after 10 iterations is: x = -3.877
Approximate integral using the trapezoidal rule: I = 0.61271
5
Published with MATLAB® R2024b