Professional Documents
Culture Documents
result:
result1 =
33
117
111
result2 =
10
13
7
result3 =
66 6 54
99 9 81
33 3 27
result4 =
66 6 54
99 9 81
33 3 27
result5 =
40
134
71
A*A and A^2 gives matrix multiplication. A.^2 gives element by element multiplication.
2.
A=[1 2 3;4 5 6;7 8 9];
B =[1;2;3];
x = A\B;
check_result = A * x;
disp('x:');
disp(x);
disp('chech A*x:');
disp(check_result);
result:
x:
-0.3333
0.6667
check A*x:
1.0000
2.0000
3.0000
3.
x1=[-1:0.001:0];
y1=x1.^2 + 2*x1;
x2=[0:0.001:1];
y2=0;
plot(x1,y1);
hold on
plot(x2,y2);
result:
4.
A = [
1 2 3;
4 5 6;
7 8 9
];
B = [
9 8 7;
6 5 4;
3 2 1
];
[rows, cols] = size(A);
result_addition = zeros(rows, cols);
for i = 1:rows
for j = 1:cols
result_addition(i, j) = A(i, j) + B(i, j);
end
end
[rows_A, cols_A] = size(A);
[rows_B, cols_B] = size(B);
if cols_A ~= rows_B
error('Matrix dimensions do not match for multiplication.');
end
for i = 1:rows_A
for j = 1:cols_B
dotProduct =0;
for k = 1:cols_A
dotProduct = dotProduct + A(i, k) * B(k, j);
end
result_multiplication(i, j) = dotProduct;
end
end
% matrix addition
disp('Matrix Addition Result:');
disp(result_addition);
% Matrix Multiplication
disp('Matrix Multiplication Result:');
disp(result_multiplication);
result:
5.
radius = 5;
height = 10;
disp(['The volume of the cone with radius ', num2str(radius), ' and height ',
num2str(height), ' is: ', num2str(volume)]);
result:
The volume of the cone with radius 5 and height 10 is: 261.7994
6.
radius=5;
theta = 0:0.01:2*pi;
x = radius * cos(theta);
y = radius * sin(theta);
plot(x, y);
axis equal;
result:
6.
f = @(x) x.^4 - 8*x.^3 + 17*x.^2 - 4*x - 20;
g = @(x) x.^2 - 4*x + 4;
h = @(x) x.^2 - 4*x - 5;
result1 = f(3) - g(3) * h(3);
disp(['Result (i) at x=3: ', num2str(result1)]);
x_values2 = [1, 2, 3, 4, 5];
result2 = f(x_values2) - g(x_values2) .* h(x_values2);
disp('Result (ii) at x=[1 2 3 4 5]:');
disp(result2);
result3= f(3) / g(3) - h(3);
disp(['Result (iii) at x=3: ', num2str(result3)]);
x_values4= -5:0.01:5;
y_f = f(x_values4);
y_ratio = f(x_values4) ./ (g(x_values4) .* h(x_values4));
figure;
plot(x_values4, y_f, 'b-', 'DisplayName', 'f(x)');
hold on;
plot(x_values4, y_ratio, 'r-', 'DisplayName', 'f(x) / (g(x) * h(x))');
result: