MatLab Beginners Examples for Numerical Analysis

matlab examples for beginners
MatLab examples for beginners

Calculate an expression using MatLab

Calculate the expression a3+√(bd) - 4c - ac, where a=45.90, b=2.3, c=4.5 and d=6.

Code

a=45.90; b=2.3; c=4.5; d=6;
ans = a.^3 + sqrt(b.*d) - 4*c - a*c;
fprintf('The answer is %4.2f ', ans)

Output

The answer is 96481.74

Generate arrays by following conditions,

(a) Array of zeroes with 10 elements (Array ‘A’)

Code

A = zeros(1, 10);
disp('Generated Array of zeros with 10 elements is: ');
disp(A);

Output

Generated Array of zeros with 10 elements is: 0 0 0 0 0 0 0 0 0 0


(b) Array a[1] = 20, a[2] = 21 and a[3] to a[10] are one (Array ‘B’)

Code

B = [20 21 ones(1, 8)];
disp('The 2nd generated is: ');
disp(B);

Output

The 2nd generated array is: 20 21 1 1 1 1 1 1 1 1 1

Inverse, Transpose, Square of Matrix using MatLab

Consider the two matrices A and B where, A = [4 -6; 6 10], B = [4.7 -23; 4.4 16] and Find the following statement using MatLab

(a) A+B

(b) B2

(c) AB

(d) (AB)T

(e) A-B

(f) A/B

(g) Inverse of A

Code

A = [4 -6; 6 10];
B = [4.7 -23; 4.4 16];
disp('Answer of A+B is:')
disp(A+B)
disp('Answer of B*B is:')
disp(B*B)
disp('Answer of AB is:')
disp(A*B)
disp('Transpose of AB is:')
disp((A*B).')
disp('Answer of A-B is:')
disp(A-B)
disp('Answer of A/B is:')
disp(A/B)
disp('Inverse of A matrix is:')
disp(inv(A))


Show all integers between 1 and 100 using MatLab for which their sine value is negative

one_to_hundred = linspace(1, 100, 100);
position = sign(sin(one_to_hundred))==1;
disp('All integers between 1 and 100 for which sine is negative are:');
disp(one_to_hundred(position));

Post a Comment

Previous Post Next Post