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

Matlab

This document contains code for performing various statistical analyses and calculations including: 1) Calculating correlation coefficients between two variables; 2) Finding regression coefficients and equations for linear regression; 3) Calculating probabilities and probability distributions; 4) Computing expectations, variances, covariances, and correlations for joint probability distributions.

Uploaded by

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

Matlab

This document contains code for performing various statistical analyses and calculations including: 1) Calculating correlation coefficients between two variables; 2) Finding regression coefficients and equations for linear regression; 3) Calculating probabilities and probability distributions; 4) Computing expectations, variances, covariances, and correlations for joint probability distributions.

Uploaded by

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

Correlation coefficient

%problem1------------------------------
x=[2,5,8,5,8,4,6,1,8];
y=[-5,2,7,-8,6,8,9,10,1];
corrcoef(x,y)

%problem2------------------------------
x=[92,89,87,86,83,77,71,63,53,50];
y=[86,83,91,77,68,85,52,82,37,57];
corrcoef(x,y)
n=length(x);
sumx=sum(x);
sumx2=sum(x.^2);
sumy=sum(y);
sumy2=sum(y.^2);
xm=sumx/n;
ym=sumy/n;
sumxy=sum(x.*y);
xym=sumxy/n;
sigmax=sqrt((sumx2/n)-xm^2);
sigmay=sqrt((sumy2/n)-ym^2);
r=(xym-(xm*ym))/(sigmax*sigmay)

curve fitting
%-------Fitting a stright line-------
x=[1,2,3,4,5,6,7,8,9];
y=[9,8,10,12,11,13,14,16,5];
n=length(x);
xpluse=sum(x);
x2pluse=sum(x.^2);
ypluse=sum(y);
xypluse=sum(x.*y);
syms a b;
eqn=[ypluse==n*a+b*xpluse; xypluse==a*xpluse+b*x2pluse];
sol=solve(eqn,a,b)
fprintf('The requried stright line is y=%.4f +%.4fx',sol.a,sol.b)
%--------Fitting a parabola---------
x=[1,2,3,4,5,6,7,8,9];
y=[9,8,10,12,11,13,14,16,5];
n=length(x);
xpluse=sum(x);
x2pluse=sum(x.^2);
x3pluse=sum(x.^3);
x4pluse=sum(x.^4);
ypluse=sum(y);
xypluse=sum(x.*y);
x2ypluse=sum(x.^2.*y);
syms a b c;
eqn=[ypluse==n*a+b*xpluse+c*x2pluse; xypluse==a*xpluse+b*x2pluse+c*x3pluse;
x2ypluse==a*x2pluse+b*x3pluse+c*x4pluse];
sol=solve(eqn,a,b,c)
fprintf('The requried parabola equation is y=%.4f +%.4fx
+%.4fx^2',sol.a,sol.b,sol.c)
NFIF
x=[1,2,3,4,5];
y=[10,26,58,112,194];
n=length(x);
X=input('Enter the value of X=');
h=x(2)-x(1);
p=(X-x(1))/h;
F=zeros(n,n);
F(:,1)=y;
for j=2:n
for i=j:n
F(i,j)=F(i,j-1)-F(i-1,j-1);
end
end
d=F(1,1)+p*F(2,2)+((p*(p-1))/factorial(2))*F(3,3)+((p*(p-1)*(p-
2))/factorial(3))*F(4,4)+((p*(p-1)*(p-2)*(p-3))/factorial(4))*F(5,5);
fprintf('The value of y(%0.4f) = %0.4f\n',X,d)

NBIF
x=[19,20,21,22,23];
y=[91,100.25,110.120.25,131];
n=length(x);
X=input("Enter X:");
h=x(2)-x(1);
q=(X-x(n))/h;
f=zeros(n,n);
f(:,1)=y;
for j=2:n
for i =j:n
f(i,j)=f(i,j-1)-f(i-1,j-1);
end
end
d=f(5,1)+q*f(5,2) + ((q*(q+1))/factorial(2))*f(5,3) + ((p*(p-1)*(p-
2))/factorial(3))*f(5,4) + ((p*(1)*(q+2)*(q+3))/factorial(4))*f(5,5);
fprintf("VALUE OF y(%0.4f)=%0.4f\n",X,d);

Simpsons rule
%----------------------simpson's 1/3rd rule----------------------------
f=@(x)(1./(1+x.^2));
a=input("Enter the lower limit a: ");
b=input("Enter the upper limit b: ");
n=input("Enter the number of sub-interval n: ");
h=(b-a)/n;
if rem(n,2)==1
fprintf('\n Please enter the valid n!!');
fprintf('\n Enter n as even number');
end
k=1:1:(n-1);
s=f(a+k.*h);
so=sum(s(1:2:n-1));
se=sum(s(2:2:n-2));
out=(h/3)*(f(a)+f(b)+4*so+2*se);
fprintf("The value of the integration using simpson's (1/3)rd rule is %.4f",out)
%----------------------simpson's 3/8th rule----------------------------
f=@(x)(1./(1+x));
a=input("\nEnter the lower limit a: ");
b=input("Enter the upper limit b: ");
n=input("Enter the number of sub-interval n: ");
h=(b-a)/n;
if rem(n,3)==1
fprintf('\n Please enter the valid n!!');
n=input('\n Enter n as multiple of 3: ');

end
k=1:1:(n-1);
s=f(a+k.*h);
out=((h*3)/8)*(f(a)+f(b)+3*(s(1)+s(2)+s(4)+s(5))+2*s(3));
fprintf("The value of the integration using simpson's (3/8)th rule is %.4f",out)

%----------------------weddle's rule------------------------------
f=@(x)(1./(1+x));
a=input("\nEnter the lower limit a: ");
b=input("Enter the limit b: ");
n=input("Enter the number of sub-interval n: ");
h=(b-a)/n;
if rem(n,6)==1
fprintf('\n Please enter the valid n!!');
n=input('\n Enter n as multiple of 6: ');
end
k=1:1:(n-1);
s=f(a+k.*h);
out=((h*3)/10)*(f(a)+5*s(1)+s(2)+6*s(3)+s(4)+5*s(5)+f(b));
fprintf("The value of the integration using weddle's rule is %.4f",out)

fourier series
%-----------------------Find the FS of f(x)=x in (-pi,pi)----------------

syms x n pi
y=x;
a0=(1/pi)*int(y,x,-pi,pi)
an=(1/pi)*int(y*cos(n*x),x,-pi,pi)
bn=(1/pi)*int(y*sin(n*x),x,-pi,pi)
FS=(a0/2)+(an*cos(n*x)+bn*(sin(n*x)))

%-----------------------Find the FS of f(x)=x^2 in (-pi,pi)----------------


syms x n pi
y=x^2;
a0=(1/pi)*int(y,x,-pi,pi)
an=(1/pi)*int(y*cos(n*x),x,-pi,pi)
bn=(1/pi)*int(y*sin(n*x),x,-pi,pi)
FS=(a0/2)+(an*cos(n*x)+bn*(sin(n*x)))
pretty(FS)
%-----------------------Find the FS of f(x)=e^(-x) in (-pi,pi)-------------
syms x n pi a
y=exp(a*-x);
a0=(1/pi)*int(y,x,-pi,pi);
an=(1/pi)*int(y*cos(n*x),x,-pi,pi);
bn=(1/pi)*int(y*sin(n*x),x,-pi,pi);
FS=(a0/2)+(an*cos(n*x)+bn*(sin(n*x)));

half range fs
%-----------------------Find the HFCS of f(x)=x(pi-x) in (-pi,pi)----------------

syms x n pi
y=x*(pi-x);
a0=(2/pi)*int(y,x,0,pi);
an=(2/pi)*int(y*cos(n*x),x,0,pi);
HFCS=(a0/2)+(an*cos(n*x));
fprintf("HFCS of x(pi-x) is: \n\n\n")
pretty(HFCS)

%-----------------------Find the HFSS of f(x)=x^2 in (-pi,pi)----------------

syms x n pi
y=x^2;
bn=(2/pi)*int(y*sin(n*x),x,0,pi);
HFSS=(bn*sin(n*x));
fprintf("HFSS of x^2 is: \n\n\n")
pretty(HFSS)

harmonic analysis
% Find the fourier coeeficient a0,a1,b1 for the following data:
% --------------------
% |x|0| 1| 2| 3| 4| 5|
% --------------------
% |y|9|18|24|28|26|20|
% --------------------
clear all;
clc;
x=[0 1 2 3 4 5];
y=[9 18 24 28 26 20];
a0=(1/3)*sum(y,2)
a1=(1/3)*sum(y.*cos(pi*x/3),2)
b1=(1/3)*sum(y.*sin(pi*x/3),2)

%-----------------------------------------------------------------------

% Find the fourier coeeficient a0,a1,b1 for the following data:


% -----------------------------------------
% |x| 0| 2| 4| 6| 8| 10| 12|
% -----------------------------------------
% |y| 9| 18.2| 24.4| 27.8| 27.5| 22.0| 9|
% -----------------------------------------
clear all;
clc;
x=[0 2 4 6 8 10];
y=[9 18.2 24.4 27.8 27.5 22];
a0=(1/3)*sum(y,2)
a1=(1/3)*sum(y.*cos(pi*x/6),2)
b1=(1/3)*sum(y.*sin(pi*x/6),2)
a2=(1/3)*sum(y.*cos(2*pi*x/6),2)
b2=(1/3)*sum(y.*sin(2*pi*x/6),2)

%-----------------------------------------------------------------------

%------------PROBABILITY---------

% P(X)=nCx * p^x * q^(n-x) x=0,1,2...

clear all;
clc;
x=0 : 10;
y=binopdf(x,10,0.5);
figure
bar(x,y,1,"blue")
xlabel('observation')
ylabel('probability')

%-----------------------------------------------------------------------
% f(x)={lam*e^(-lam * x) ,x>=0
% {0 ,otherwise

clear all;
clc;
x=0 :0.1: 20;
y=exppdf(x,2);
figure
plot(x,y)
xlabel('observation')
ylabel('probability')

%---------------------------------------------------------------------
% f(x)= (1/(sig*sqrt(2*pi))) * e(-(1/2)*((x-u)/sig)^2) ,-infi<x<infi

clear all;
clc;
x=-50 :0.1: 50;
mu=2;
sigma=1;
y= normpdf(x,mu,sigma);
plot(x,y)
xlabel('observation')
ylabel('probability')

dfd
clear all;
clc;
x=-50 :0.1: 50;
mu=2;
sigma=1;
y= normpdf(x,mu,sigma);
plot(x,y)
xlabel('observation')
ylabel('probability'
regression_coeffecient
%problem1------------------------------
x=[2,5,8,5,8,4,6,1,8];
y=[-5,2,7,-8,6,8,9,10,1];
corrcoef(x,y)
n=length(x);
sumx=sum(x);
sumx2=sum(x.^2);
sumy=sum(y);
sumy2=sum(y.^2);
xm=sumx/n;
ym=sumy/n;
sumxy=sum(x.*y);
xym=sumxy/n;
sigmax=sqrt((sumx2/n)-xm^2);
sigmay=sqrt((sumy2/n)-ym^2);
r=(xym-(xm*ym))/(sigmax*sigmay);
byx=(r*sigmay)/sigmax;
a=ym-(byx*xm);
disp("The Regression line is Y= "+ byx +"X + "+a)
y1=a+(byx*x);
scatter(x,y)
hold on
plot(x,y1)
hold off

%problem2------------------------------
x=[92,89,87,86,83,77,71,63,53,50];
y=[86,83,91,77,68,85,52,82,37,57];
corrcoef(x,y)
n=length(x);
sumx=sum(x);
sumx2=sum(x.^2);
sumy=sum(y);
sumy2=sum(y.^2);
xm=sumx/n;
ym=sumy/n;
sumxy=sum(x.*y);
xym=sumxy/n;
sigmax=sqrt((sumx2/n)-xm^2);
sigmay=sqrt((sumy2/n)-ym^2);
r=(xym-(xm*ym))/(sigmax*sigmay);
byx=(r*sigmay)/sigmax;
a=ym-(byx*xm);
disp("The Regression line is Y= "+ byx +"X + "+a)
y1=a+(byx*x);
scatter(x,y)
hold on
plot(x,y1)
hold off
joint probability
%---------------------Joint probabilty distriution-----------------
% |x/y| 2 | 3 | 4 |
% --------------------
% | 1 |0.06|0.15|0.09|
% --------------------
% | 2 |0.14|0.35|0.21|
% --------------------
% Find E(x),E(y),E(x^2),E(y^2),E(xy),sigma(x),sigma(y),cov(x,y),rho(x,y)

clear all;
clc;
x=[1 2];
y=[2 3 4];
fx=[0.3 0.7];
gy=[0.2 0.5 0.3];
x2=x.^2;
y2=y.^2;
Ex=sum(x.*fx)
Ey=sum(y.*gy)
Ex2=sum(x2.*fx)
Ey2=sum(y2.*gy)
Exy=(1*2*0.06 + 1*3*0.15 + 1*4*0.09 + 2*2*0.14 + 2*3*0.35 + 2*4*0.21)
cov=Exy-Ex*Ey
varX=Ex2-(Ex)^2
varY=Ey2-(Ey)^2
SDx=sqrt(varX)
SDy=sqrt(varY)
correlation=cov/(SDx*SDy)

% |x/y| 1 | 3 | 9 |
% --------------------
% | 2 | 1/8|1/24|1/12|
% --------------------
% | 4 | 1/4| 1/4| 0|
% --------------------
% | 6 | 1/8|1/24|1/12|
% --------------------
% Find E(x),E(y),E(x^2),E(y^2),E(xy),sigma(x),sigma(y),cov(x,y),rho(x,y)
clear all;
clc;
x=[2 4 6];
y=[1 3 9];
fx=[6/24 1/2 6/24];
gy=[4/8 8/24 2/12];
x2=x.^2;
y2=y.^2;
Ex=sum(x.*fx)
Ey=sum(y.*gy)
Ex2=sum(x2.*fx)
Ey2=sum(y2.*gy)
Exy=(2*1*(1/8) + 2*3*(1/24) + 2*9*(1/24) + 4*1*(1/4) + 4*3*(1/4) + 4*9*0 +
6*1*(1/8) + 6*3*(1/24) + 6*9*(1/24))
cov=Exy-Ex*Ey
varX=Ex2-(Ex)^2
varY=Ex2-(Ey)^2
SDx=sqrt(varX)
SDy=sqrt(varY)
correlation=cov/(SDx*SDy)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy