Академический Документы
Профессиональный Документы
Культура Документы
C MATLAB
C is considered as High Level Language MATLAB is High Level Language.
as well as Middle Level Language.
In C in 2D Array, blocks are indexed In MATLAB in 2D Array, blocks are
from Left to Right and then Up to indexed from Up to Down and then Left
Down. to Right.
In C, index starts from 0. In MATLAB, index starts from 1.
In C, Lines of Code are more as In MATLAB, Lines of code are Less as
compare to MATLAB for the same compare to C for the same program.
program.
MATLAB is an interactive program for numerical computation and data visualization. You can
enter a command by typing it at the MATLAB prompt '>>' on the Command Window.
COMMAND PURPOSE
MATLAB provides various useful commands for working with the system, like saving the current
work in the workspace as a file and loading the file later. It also provides various commands for
other system-related activities like, displaying date, listing files in the directory, displaying current
directory, etc.
The following table displays some commonly used system-related commands –
COMMAND PURPOSE
COMMAND PURPOSE
The fscanf and fprintf commands behave like C scanf and printf functions. They support the
following format codes −
%s Format as a string.
%d Format as an integer.
“All variables of all data types in MATLAB are multidimensional arrays. A vector is a one-
dimensional array and a matrix is a two-dimensional array.”
Array can be declared as:
a=[1, 2 ,3; 4,5,6]
A magic square is a square that produces the same sum, when its elements are added row-wise,
column-wise or diagonally. The magic( ) function creates a magic square array. It takes a singular
argument that gives the size of the square. The argument must be a scalar greater than or equal to 3.
end
for i=1:n+1
s=0;
for j=1:n
s=s+a(i,j);
end
a(n+1,i)=s;
end
8 1 6
3 5 7
4 9 2
a =
8 1 6 15
3 5 7 15
4 9 2 15
15 15 15 45
Conditional Statements:
Conditional statements enable you to select at run time which block of code to execute.
if (condition 1)
%do this
elseif (condition 2)
%do this
else
%do this
end
for loop to repeat specified number of times
for index = values, statements, end executes a group of statements in a loop for a specified number
of times. values has one of the following forms:
initVal:endVal — Increment the index variable from initVal to endVal by 1, and repeat execution
of statements until index is greater than endVal.
initVal:step:endVal — Increment index by the value step on each iteration, or
decrements index when step is negative.
valArray — Create a column vector, index, from subsequent columns of array valArray on each
iteration. For example, on the first iteration, index = valArray(:,1). The loop executes a maximum
of n times, where n is the number of columns of valArray, given by numel(valArray(1,:)). The
input valArray can be of any MATLAB® data type, including a character vector, cell array, or
struct.
a=input('Enter a: ')
b=input('Enter b: ')
c=input('Enter c: ')
d=b*b-4*a*c;
if(d>0)
disp('Two Real Solution. ')
ans1=(-b+sqrt(d))/2*a
ans2=(-b-sqrt(d))/2*a
elseif(d==0)
disp('One Real Solution')
ans1=(-b)/2*a
else
disp('It Has No Solution')
end
Enter a: > 1
a = 1
Enter b: > 3
b = 3
Enter c: > 1
c = 1
Two Real Solution.
ans1 = -0.38197
ans2 = -2.6180
plot(X,Y)
plot(X,Y,LineSpec)
plot(X1,Y1,...,Xn,Yn)
plot(X1,Y1,LineSpec1,...,Xn,Yn,LineSpecn)
plot(Y)
plot(Y,LineSpec)
plot(___,Name,Value)
plot(ax,___)
h = plot(___)
If X and Y are both vectors, then they must have equal length. The plot function plots Y versus X.
If X and Y are both matrices, then they must have equal size. The plot function plots columns
of Y versus columns of X.
If one of X or Y is a vector and the other is a matrix, then the matrix must have dimensions such
that one of its dimensions equals the vector length. If the number of matrix rows equals the vector
length, then the plot function plots each matrix column versus the vector. If the number of matrix
columns equals the vector length, then the function plots each matrix row versus the vector. If the
matrix is square, then the function plots each column versus the vector.
If one of X or Y is a scalar and the other is either a scalar or a vector, then the plot function plots
discrete points. However, to see the points you must specify a marker symbol, for example,
plot(X,Y,'o').
plot(X,Y,LineSpec) sets the line style, marker symbol, and color.
plot(X1,Y1,...,Xn,Yn) plots multiple X, Y pairs using the same axes for all lines.
plot(X1,Y1,LineSpec1,...,Xn,Yn,LineSpecn) sets the line style, marker type, and color for
each line. You can mix X, Y, LineSpec triplets with X, Y pairs. For
example, plot(X1,Y1,X2,Y2,LineSpec2,X3,Y3).
Y yellow G green
M magenta B blue
C cyan W white
R red K black
Y = sin(x); X = wt; 0<=t<=0.4; W=2πf; F=50;
f=50;
w=(2*pi*f);
t=0:0.001:0.04;
x=w*t;
y=sin(x);
plot(x,y,'k:h','LineWidth',2.5,'MarkerSize',3,'MarkerEdgeColor','g');
hold on;
plot(y,x,'r:h','LineWidth',5,'MarkerSize',4,'MarkerEdgeColor','k');
hold off;
title 'sine';
xlabel 'x:wt:t:0-0.004';
ylabel 't:0-0.04';
grid;
>>for 3D graph
Plot3(x,y,z)
>>for bargraph
bar(x,y)
>>for horizontal bargraph
barh(x,y)
>>for piechart
Pie(x,y)
>>to display multiple plots in a single figure
subplot(2,2,1)
“Monte Carlo “simulation is a technique used to study how a model responds to randomly generated
inputs. It typically involves a three-step process:
The MATLAB language provides a variety of high-level mathematical functions you can use to
build a model for Monte Carlo simulation and to run those simulations. MATLAB is used for
financial modelling, weather forecasting, operations analysis, and many other applications.
s=0;
c=0;
for i=1:10000
x=rand-0.5;
y=rand-0.5;
if(x<=0.5 && x>=-0.5)
if(y<=0.5 && y>=-0.5)
s=s+1;
plot(x,y,'r+');
end
end
pi=4*c/s;
disp(pi);
Pi = 3.1420
n=13;
for i=1:n
a=sort(rand(n,1))*2*pi;
r=randi(9,1,1);
x=cos(a).*r
y=sin(a).*r;
end
d=sort(y);
e=sort(x);
rady=(d(n)-d(1))/2;
radx=(e(n)-e(1))/2;
centx=(e(n)+e(1))/2;
centy=(d(n)+d(1))/2;
inside=0;
n1=1200;
while(n1>0)
p=e(n)*rand;
q=d(n)*rand;
if(radx>=rady)
if((q-centy)<sqrt((radx*radx)-((p-centx)*(p-centx))))
inside=inside+1;
areac=pi*(radx*radx);
% plot(p,q,'r.');
end
end
if(rady>radx)
if((q-centy)<sqrt((rady*rady)-((p-centx)*(p-centx))))
inside=inside+1;
areac=pi*(rady*rady);
% plot(p,q,'r.');
end
end
% hold on;
n1=n1-1;
end
%hold off;
ans=areac*(inside)/1200;
fprintf('%f',ans);