Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
3 views5 pages

Matlab Codes

The document provides a comprehensive guide to various MATLAB functionalities, including arithmetic operations, matrix manipulations, plotting techniques, sorting, and solving mathematical expressions. It also covers predefined keywords, set operations, subplot usage, matrix inversions, data insertion into Excel, loops, and conditional statements. Each section includes examples of commands and their applications for effective programming in MATLAB.

Uploaded by

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

Matlab Codes

The document provides a comprehensive guide to various MATLAB functionalities, including arithmetic operations, matrix manipulations, plotting techniques, sorting, and solving mathematical expressions. It also covers predefined keywords, set operations, subplot usage, matrix inversions, data insertion into Excel, loops, and conditional statements. Each section includes examples of commands and their applications for effective programming in MATLAB.

Uploaded by

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

Matlab codes

1 Arithmetic and logarithmic


+,-,/.* and .*(for scalar multiplication or element vise multiplication)
Log(Value),sin(Value)
For inverse of transedencial equations use asin, acos, atan
Pow(Value)
2 Matrices
We can use variable to form a matrix
A=[values,values];B=[values,values];
To find the max numbers of column we use command
max(a)
To find only one max digit use
max(max(a))
To find the location of matrix number;
[max location] = max(a)
3 Plots
To plot the graph;
Plot(x,y)
Bar(x,y)
Default it is vertical to draw a horizontal bar graph;
Barh(x,y)
Pie(x,y)
Hist(x)
Comet(x)
To change the line of plot;
Plot(x,y,’--’)
We can use either **,++ in place of –
To change the colour of graph line;
Plot(x,y,’r’) (For red colour line)
We can also use r,b,g for red,blue,green
To obtain 2 graph at a single time;
Plot(x,y1)
hold on
plot(x,y2)
we can use legend command to specify the lines of graph
imp(we only use legend after two graph sets)
legend(‘first graph line name’,’second graph line name’)
to plot a 3d graph use the command ;
plot3(x,y);
4 Sorting
Sorting is applied to matices or vectors. It is used to ascend or descend numbers
Sort(a,1) to obtain ascending order(column vice sorting)
Sort(a,2) give (row vice sorting)
Sort(a,’descend’)
To find the non zero element of a matrix
We use n=find(a) this command give us the index value. To read the index value we read it by
column vice.

5 Solving mathematical expressions


Y=a*x+b (for noly one answer)
Y=a.*x+b (for element vice mulyiplication)
Y=a.ex written as y=a*exp(x)
Y=loga logb written as y=log(a)*log(b)
If we write log only in matlab so its mean that consider as a natural log but if we write log10 so it
takes base log.
6 Predefined keywords in Matlab
6.1 Clc
(to clear the command window)
6.2 Disp
To display the any word you have to write it as; disp(‘-------------------------’)
6.3 Clear all
To clear the workspace
6.4 Close all
To close the all opened file
6.5 Input
To assigned the variable as; y=input(‘value of x’)
6.6 Sort
To ascend or descend the matrix or array
6.7 Power
Power(2,3)
Means 2(variable) to raise power 3 (23)
6.8 Zeros
A=zeros(2,4)
6.9 ones
a=ones(2,4)
where 2 is the number of rows
4 is the number of columns
6.10 magic
a=magic(4)
means 4*4 matrix automatically formed upto 16 digits
6.11 sqrt
to take the square root of any number
6.12 floor
to obtain the nearest whole number as floor(x)
6.13 ceil
to obtain the upper value of any number which is written as integer ceil(x)

7 Set operation
Union and intersection
C=union(a,b)
C=union(a,b,’rows’) to obtain the rows vice union
C=intersect(a,b)
C=intersect(a,b,’rows’) to obtain the rows vice intersection

8 Subplot
Subplot is used to plot different numbers of graph at a single window
Commands; (i) subplot(x,y,p);
(ii) Plot(x,y);
Where x, y in subplot is the numbers of quadrants and p is the position of graph
Where x, y in plot is the elements of graph
Using figure; before subplot will plot the graph in separate windows
When we use figure it must be remember that % sign will be used before subplot

9 Matrix
For taking inverse of a matrix we use inv(x);
AB-1 can be written as A/B

10 Size and length


D=size(x);

Length(x);

rand(2,1)
to costumize the range of random numbers we use formula
x=a+(b-a)*rand(2,2)

11 how to insert data in excel sheet?


1st command;
Xlswrite(‘filename.xlsx’,enter name of values)
To edit the header of excel sheet
Headers={‘first’,’second’,’third’}
Xlswrite(‘filename.xlsx’,[headers ; values]);
For raeding xl sheet
Xlsread(‘filename.xlsx’)

12 Creating loop
For a=1:2:20

Disp(a)

End

13 If or else condition
If x>y

Disp(‘yes’)

Else

Disp(‘not’)

End

We can also use elseif condition

We can also use and (&), or (|) condition in it

You might also like