0% found this document useful (0 votes)
32 views6 pages

Matlab

The document contains examples of matrix operations in MATLAB including matrix multiplication, addition, element-wise operations, and solving systems of linear equations. It also contains examples of plotting functions and calculating volumes of geometric shapes.

Uploaded by

Common Man
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views6 pages

Matlab

The document contains examples of matrix operations in MATLAB including matrix multiplication, addition, element-wise operations, and solving systems of linear equations. It also contains examples of plotting functions and calculating volumes of geometric shapes.

Uploaded by

Common Man
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

1.

A=[1 2 3;5 7 8;4 9 2];


B=[6;9;3];
C=[11 1 9];
result1=A*B
result2=B+4
result3=B*C
result4=B.*C
result5=A*C'

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

result_multiplication = zeros(rows_A, cols_B);

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:

Matrix Addition Result:


10 10 10
10 10 10
10 10 10

Matrix Multiplication Result:


30 24 18
84 69 54
138 114 90

5.

function volume = conevolume(radius, height)


volume = (1/3) * pi * radius^2 * height;
end

radius = 5;
height = 10;

volume = conevolume(radius, height);

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:

Result (i) at x=3: -6

Result (ii) at x=[1 2 3 4 5]:


-6 -8 -6 0 10

Result (iii) at x=3: -6

You might also like