0% found this document useful (0 votes)
7 views8 pages

Matlab Codes for Statistical Analysis

Uploaded by

lakulish396
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)
7 views8 pages

Matlab Codes for Statistical Analysis

Uploaded by

lakulish396
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

ALL Matlab Codes:

1)Mean
n=input('enter number of observations')
sumx=0
for i=1:n
xi=input('enter the value of observations')
sumx=sumx+xi
end
average=sumx/n

n=input('enter number of observations')


sum1=0;
sum2=0;
for i=1:n
xi=input('enter value of observations')
fi=input('enter value of frequencies')
sum1=sum1+(fi*xi)
sum2=sum2+fi
end
average=sum1/sum2

n=input('enter number of observations')


sum1=0;
sum2=0;
for i=1:n
ui=input('enter the value of upperlimit of class')
li=input('enter the value of lowerlimit of class')
xi=(ui+li)/2
fi=input('enter value of frequencies')
sum1=sum1+(fi*xi)
sum2=sum2+fi
end
average=sum1/sum2

m=input('enter case no.')


switch m
case 1
n=input('enter number of observations')
sumx=0
for i=1:n
xi=input('enter the value of observations')
sumx=sumx+xi
end
average=sumx/n
case 2
n=input('enter number of observations')
sum1=0;
sum2=0;
for i=1:n
xi=input('enter value of observations')
fi=input('enter value of frequencies')
sum1=sum1+(fi*xi)
sum2=sum2+fi
end
average=sum1/sum2
case 3
n=input('enter number of observations')
sum1=0;
sum2=0;
for i=1:n
ui=input('enter the value of upperlimit of class')
li=input('enter the value of lowerlimit of class')
xi=(ui+li)/2
fi=input('enter value of frequencies')
sum1=sum1+(fi*xi)
sum2=sum2+fi
end
average=sum1/sum2
end

2)Median:
n=input('enter number of observations')
data=[];
for i=1:n
x=input(sprintf("%d:",i))
data=[data,x]
end
sort(data)
fprintf('the median is:')
if rem(n,2)==1
disp(data(floor(n+1)/2));
else
disp((data(floor(n/2))+data(floor(n/2)+1))/2);
end

n=input('no. of observation');
for i=1:n
l(i)=input('lower limit:');
u(i)=input('upper limit:');
f(i)=input('frequency:');
cf(1)=f(1);
h=u(i)-l(i);
end
for i=2:n
cf(i)=cf(i-1)+f(i);
end
N=cf(n)
pos=1;
for i=1:n
if cf(i)<=(N/2)
pos=pos+1
else
break
end
end

fp=f(pos);
cfp=cf(pos-1);
lp=l(pos);

m=lp+(((N/2)-cfp)/fp)*h

n=input('no. of observation');
h=input('class size');
l=input('lower limit');
u=l+h
f=input('frequency');
for i=2:n
l(i)=u(i-1)
u(i)=l(i)+h
f(i)=input('frequency:');
cf(1)=f(1);
end
for i=2:n
cf(i)=cf(i-1)+f(i);
end
N=cf(n)
pos=1;
for i=1:n
if cf(i)<=N/2
pos=pos+1
else
break
end
end

fp=f(pos);
cfp=cf(pos-1);
lp=l(pos);

m=lp+(((N/2)-cfp)/fp)*h

3)Mode
n=input('enter [Link] observations')
h=input('enter class size')
for i=1:n
l(i)=input('lower limit:');
u(i)=input('upper limit:');
f(i)=input('frequencies:');
end
[xm,idx]=max(f);
f0=f(idx-1);
f2=f(idx+1);
f1=f(idx);
l=l(idx);
Z=l+((f1-f0)/((2*f1)-f0-f2))*h
disp(z)

4) Quadratic Equation
n=input('enter [Link] observations')
a= input("Co-efficient of x^2:");
b= input("Co-efficient of x:");
c=input("Contant term:");
d= b^2-4*a*c;
fx=[a b c];
x=roots(fx);
if d>0
disp(['The equation has two roots']);
elseif d==0
disp(['The equation has one root']);
else d<0
disp("The equation has no real roots.");
end

5)Karl Pearson
n=input('[Link] observations')
sumx=0;
sumy=0;
sumxy=0;
x2=0;
y2=0;
for i=1:n
x(i)=input("value of x:");
y(i)=input("value of y:");
z(i)=x(i)*y(i)
sumx=sumx+x(i);
sumy=sumy+y(i);
sumxy=sumxy+z(i);
x2=x2+(x(i))^2;
y2=y2+(y(i))^2;
end
disp(x)
disp(y)
d=sqrt((n*x2-(sumx)^2)*(n*y2-(sumy)^2));
r=((n*sumxy)-sumx*sumy)/d

6) Standard Deviation
n=input("[Link] observation:");
sumx=0;
sumy=0;
for i=1:n
x(i)=input("value of x:");
sumx=sumx+x(i);
end
mean=sumx/n;
for i=1:n
a=x(i)-mean;
sumy=sumy+a^2;
end
standard=sqrt(sumy/(n-1))

n=input('enter [Link] observations')


for i=1:n
l(i)=input('lower limit:');
u(i)=input('upper limit:');
f(i)=input('frequencies:');
end
for i=1:n
x(i)=input("value of x:");
sumx=sumx+x(i);
end
mean=sumx/n;
for i=1:n
a=x(i)-mean;
sumy=sumy+(a^2(f));
standard=sqrt(sumy/(n-1))

7) Spearman
n=input("[Link] observations:");
for i=1:n
x(i)=input("x:");
y(i)=input("y:");
end
[RHO,PVAL]=corr(x',y','type','spearman')
scatter(x,y)

8)Regression Line
n=input('Enter number of observations:');
sumx=0;
sumy=0;
sumxy=0;
sumyy=0;
for i=1:n
x(i)=input('Enter value of x:');
y(i)=input('Enter value of y:');
sumx = sumx +x(i);
sumy= sumy + y(i);
sumxy = sumxy + (x(i)*y(i)) ;
sumyy = sumyy + (y(i)*y(i));
end
b=((n*sumxy-sumx*sumy)/(n*sumyy-sumy*sumy));
a=((sumx-b*sumy)/n);
X=b*y+a;
disp(' ');
fprintf("X=%0.2f Y+ %0.2f",b,a);
plot(X,y)

n=input('enter the obs');


sum1=0;
sum2=0;
sum3=0;
sum4=0;
sum5=0;
for i=1:n
x=input('enter the x');
y=input('enter the y');
sum1=sum1+x*y ;
sum2=sum2+x;
sum3=sum3+y;
sum4=sum4+x*x;
sum5=sum5+y*y;
end
Byx=(n*sum1-(sum2*sum3))/(n*sum4-(sum2*sum2))

9) Curve Fittings
a)y=a*e(b*x)
n=input("Enter no of observations:");
for i=1:n
x(i)=input("enter xi:");
y(i)=input("enter yi:");
end
sumx=0;
sumy=0;
sumxy=0;
sumx2=0;
for i=1:n
sumx=sumx+x(i);
sumy=sumy+log(y(i));
sumx2=sumx2+(x(i)^2);
sumxy=sumxy+(x(i)*log(y(i)));
end
b=(n*sumxy-sumx*sumy)/(n*sumx2-(sumx)^2);
A=(sumy-b*sumx)/n;
a=exp(A);
y1=a*exp(b*x);
fprintf("y=%0.3fe^(%0.3fx)",a,b);
plot(x,y1);
b)y=a*b^x
n=input("Enter no of observations:");
for i=1:n
x(i)=input("enter xi:");
y(i)=input("enter yi:");
end
sumx=0;
for i=1:n
sumx=sumx+x(i);
end
sumY=0;
sumxY=0;
sumx2=0;
for i=1:n
sumY=sumY+log(y(i));
sumxY=sumxY+x(i)*log(y(i));
sumx2=sumx2+(x(i)^2);
end
B=(n*sumxY-sumx*sumY)/(n*sumx2-(sumx)^2);
A=(sumY-B*sumx)/n; a=exp(A); b=exp(B);
y1=a*(b.^x);
fprintf("y=%0.3f(%0.3f)^x",a,b);
plot(x,y1);

c)Polynomial
disp("[Link] Quadratic equation:y=a*(x)^2+b*x+c");
n=input("Enter no of observations:");
for i=1:n
x(i)=input("enter xi:");
y(i)=input("enter yi:");
end
sumx=0;
sumy=0;
sumxy=0;
sumx2=0;
sumx3=0;
sumx2y=0;
sumx4=0;
for i=1:n
sumx=sumx+x(i);
sumy=sumy+y(i);
sumxy=sumxy+x(i)*y(i);
sumx2=sumx2+(x(i)^2);
sumx3=sumx3+(x(i)^3);
sumx4=sumx4+(x(i)^4);
sumx2y=sumx2y+((x(i)^2)*y(i));
end
mat=[sumx2 sumx n sumy;sumx3 sumx2 sumx sumxy;sumx4 sumx3 sumx2 sumx2y];
rmat=rref(mat);
a=rmat(1,4);
b=rmat(2,4);
c=rmat(3,4);
Y=a*x.^2+b*x+c;
fprintf("y=%0.3fx^2+%0.3fx+%0.3f",a,b,c);
plot(x,Y);

d) y=a*x^b
n=input("Enter no of observations:");
for i=1:n
x(i)=input("enter xi:");
y(i)=input("enter yi:");
end
sumx=0;
sumy=0;
sumxy=0;
sumx2=0;
for i=1:n
sumx=sumx+log(x(i));
sumy=sumy+log(y(i));
sumxy=sumxy+(log(x(i))*log(y(i)));
sumx2=sumx2+(log(x(i))^2);
end
b=(n*sumxy-sumx*sumy)/(n*sumx2-(sumx)^2);
A=(sumy-b*sumx)/n;
a=exp(A);
y1=a*(x.^b);
fprintf("%0.3fx^%0.3f",a,b);
plot(x,y1);

10) Probability Distribution


n=input('enter n:');
p=input('enter p:');
x=input('enter x:');
q=1-p;
pbf=(factorial(n)/(factorial(x)*factorial(n-x)))*p.^(x)*q.^(n-x)

You might also like