Q1 - Program
Q1 - Program
Q1 - Program
clc; clear all; disp 'Creating Row Vector'; x = 1:1:100; disp 'Creating Column Vector'; y = [1;2;3] disp 'Creating Matrix' a = [1 ,2 ,3 ; 4 ,5 ,6 ; 7 ,8 ,9 ] disp 'Taking Transpose of a' a' disp 'Taking Inverse of a' a^-1
Q2 Program
clc; clear; clear all; disp 'Creating Matrix'; a = [1,2,3;4,5,6;7,8,9] disp 'Changing value at (3,3), 9 to 10' a(3,3)=10 disp 'Adding an 9 at (4,4)' a(4,4) = 9 disp 'Deleting first row' a(1,:) = []
Q2 Output
Creating Matrix a= 1 4 7 2 5 8 3 6 9
Adding an 9 at (4,4) a= 1 4 7 0 2 5 8 0 3 6 10 0 0 0 0 9
Q3 Program
clc; clear; clear all; disp('Creating Matrix a') a=[1,2,3;4,5,6;7,8,9] disp('Creating Matrix b') b=[4,5,6;7,8,9;1,2,3] disp('Sum') a+b disp('Subtract') a-b disp('Multiply') a*b disp('Division') a/b disp('Multiply Element by Element') a.*b disp('Divide Element by Element') a./b
Q3 Output
Creating Matrix a a= 1 4 7 2 5 8 3 6 9
Multiply ans = 21 27 33 57 72 87 93 117 141 Division Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 9.251859e-18. > In q3 at 15 ans = 0 1 0 0 0 1 1 0 0
Multiply Element by Element ans = 4 10 18 28 40 54 7 16 27 Divide Element by Element ans = 0.2500 0.5714 7.0000 0.4000 0.6250 4.0000 0.5000 0.6667 3.0000
Linspace
Syntax: linspace(x1, x2) , linspace(x1, x2, n) where x1 and x2 are two numbers and n is the number of points between x1& x2. linspace(x1,x2) gives a linear vector of 100 length, for points in between x1 and x2 linspace(x1,x2,n) gives a linear vector of n length, with points in between x1 and x2.
Logspace
Syntax: logspace(x1,x2), logspace(x1,x2,n) logspace (x1,x2) gives 50 logarithmically spaced values logspace(x1,x2,n) gives n logarithmically spaced valves between x1 & x2
Log
Syntax: log(number) returned logarithmic value of number with base e
Log10
Syntax: log10(number) returned logarithmic value of number with base 10
Exponential
Syntax: exp(number) It is an exponential valve of number
Square root
Syntax: sqrt(number) It returns square root of number given.
Trigonometric Functions
Syntax: sin(number), cos(number) It returns trigonometric value of corresponding function.
Plot
Syntax: plot(x,y) It plots values of x and y and generates a figure output in form of chart. Various types of plots can be generated using this plot command.
Input
Syntax: input('message to display on screen') This command is used to take the input from user. Message written in box will be displayed first and then the input from the used is taken. String input can be also taken from used by adding 's' in the input() ex: input('input a string','s')
Display
Syntax: disp('message to display') This command is used to display a message on the command screen. It can be also used to display a value of any variable too.
Using load command we can restore all those variable back in to workspace from the binary MAT-file
Q4 Program
% plot t ing s in (x ) f o r x 0 to 2 p i clc; clear; clear all; x = linspace(0,(2*pi)); y = sin(x); plot(x,y); title('Plot Created by Vivek'); xlabel('x-axis') ylabel('Sin(x)')
Q5 Program
%Program to draw the circle of specified radius clc; clear; clear all; clear fig; r = input('radius> '); th = linspace(0,(2*pi),1000); x = r*cos(th); y = r*sin(th); plot(x,y); axis('equal') title('Circle')
Q6 Program
%Fibonacci clc; clear; clear all; clear fig; a = 0; b = 1; r = []; x = input('Enter number of Terms> '); for i=1:x s = a + b; a = b; b = s; r(1,i) = b; end disp(r);
Q6 Output
Enter number of Terms> 10 1 2 3 5 8 13 21 34 55 89
Q7 Program
%geometric series clc; clear; clear all; r = input('value of r > '); n = input('value of n > '); sum = 0; for i=0:n sum = sum + r^i; end disp('Sum of grometric series is') disp(sum)
Q7 Output
value of r > 3 value of n > 7 Sum of grometric series is 3280