Professional Documents
Culture Documents
Complete PSLP
Complete PSLP
PROGRAMMING LAB
PRACTICAL FILE
Date of Date of
S. No Experiment checking Signature Remark
Performance
Source Code:
m=3;
n=3;
A=[1 2 3; 4 5 6; 7 8 9;]
B=[10 20 30; 40 50 60; 70 80 90;]
for i=1:m
for j=1:n
C(i,j)=A(i,j)+B(i,j);
end
end
disp("Matrix A: ");
disp(A);
disp("Matrix B: ");
disp(B);
disp("Sum Matrix C: ");
disp(C);
disp("Vivek Mishra")
m=3;
n=3;
A=[1 2 3; 4 5 6; 7 8 9;]
B=[10 20 30; 40 50 60; 70 80 90;]
for i=1:m
for j=1:n
C(i,j)=A(j,i);
end
end
disp("Matrix A: ");
disp(A);
disp("Transpose of Matrix A : ");
disp(C);
disp("Vivek Mishra");
Source Code:
x = -30:30;
y = x.^3;
[a, b] = reglin(x, y);
plot(x, y, "red")
plot(x, a*x+b)
disp(“Vivek Mishra”)
Output:
EXPERIMENT NO 5
AIM: To obtain the first four moments about Mean
Source Code:
clc
n=input('Enter the no. of observations:');
disp('Enter the values of xi');
for i=1:n
x(i)=input('\');
end;
disp('Enter the corresponding frequences fi:')
sum=0;
for i=1:n
f(i)=input('\');
sum=sum+f(i);
end;
r=input('How many moments to be calculated:');
sum1=0
for i=1:n
sum1=sum1+f(i)*x(i);
end
A=sum1/sum;
//Calculate the average
disp('Average=%f\n',A);
for j=1:r
sum2 =0;
for i=1:n
y(i)=f(i)*(x(i)-A)^j;
sum2=sum2+y(i);
end
M(j)=(sum2/sum);//Calculate the moments
disp('Moment about mean M(%d)=%f\n',j,M(j));
end
sd=sqrt(M(2));//Calculate the standard deviation
disp('Standard deviation=%f\n',sd);
disp(“Vivek Mishra”);
Output:
EXPERIMENT NO 6
AIM: Solve a LPP of three variables using Simplex Method
Source Code:
function [f, g, ind]=rosenbrock(x, ind)
f=100*(x(2)-x(1)^2)^2+(1-x(1))^2;
g(1)=-400.*(x(2)-x(1)^2)*x(1)-2.*(1.-x(1))
g(2)=200.*(x(2)-x(1)^2)
endfunction
x0=[-1.21];
[x,f]=fminsearch(rosenbrock,x0);
disp("x = %s\n",strcat(string(x)," "));
disp("f = %e\n",f);
disp(“Vivek Mishra”)
Output:
EXPERIMENT NO 7
AIM: Solve a transportation problem of three variables
Source Code:
clc;
nCanneries = 2;
nWarehouses = 3;
ShippingCost = [2.5 1.7 1.8; 2.5 1.8 1.4];
demand = [300 300 300]';
supply = [350 650]';
nVar = nCanneries*nWarehouses;
lb = zeros(1,nVar);
for i = 1:nCanneries
indeX = (i-1)*nWarehouses+1:i*nWarehouses;
As(i,indeX) = ones(1,nWarehouses);
end
Bs = supply;
As
for i = 1:nWarehouses
indeX = i:nWarehouses:nVar;
Ad(i,indeX) = -1;
end
Bd = -demand;
A = cat(1,As,Ad);
b = cat(1,Bs,Bd);
Cost = zeros(nVar,1);
for i = 1:nCanneries
indeX = (i-1)*nWarehouses+1:i*nWarehouses;
Cost(indeX) = (ShippingCost(i,:)');
end
[xopt,fopt,exitflag,output,lambda] = linprog(Cost,A,b,[],[],lb,[]);
if exitflag == 0 then
disp(" Optimal Solution Found")
for i = 1:nCanneries
indeX = (i-1)*nWarehouses+1: i*nWarehouses;
X(i,1:nWarehouses) = xopt(indeX,1)';
end
Values = string(X); Markets(1,1) = [" "];
for j = 2:nWarehouses+1
Markets(1,j) = strcat(["M_",string(j-1)]);
End
for i = 1:nCanneries
Plants(i) = strcat(["P_",string(i)]);
end
matrix(ut.string,size(table))
elseif exitflag == 1 then
disp('Primal Infeasible')
elseif exitflag == 2 then
disp("Dual Infeasible")
else
disp("Technical error encountered")
end
disp(“Vivek Mishra”)
Output:
EXPERIMENT NO 8
AIM: Solve an Assignment problem of three variables
Source Code:
clc;
nProjects = 5;
nContractors = 3;
bids = [20 10000 10 9 15;18 12 13 8 16; 10000 11 12 7 17];
reqResource = [70 10000 40 30 50;65 40 40 35 55; 10000 45 35 40 50];
availResource = [120; 100; 70];
nVar = nProjects*nContractors;
IntCon = 1:nVar;
lb = zeros(1,nVar);
ub = ones(1,nVar);
beq = ones(nProjects,1);
for i = 1:nProjects
Aeq(i,i:nProjects:nVar) = 1;
b = availResource;
for j = 1:nContractors
index = (j-1)*nProjects+1:j*nProjects;
A(j,index) = reqResource(j,:);
end
for j = 1:nContractors
index = (j-1)*nProjects+1:j*nProjects;
Cost(index,1) = bids(j,:)';
end
options = list("time_limit", 2500);
[xopt,fopt,status,output] = symphonymat(Cost,IntCon,A,b,Aeq,beq,lb,ub,options);
disp(output)
for j = 1:nContractors
index = (j-1)*nProjects+1:j*nProjects;
Contractor(j).Project = string(find(xopt(index)==1));
end
ResolurceUtilized = ((A*xopt)./b)*100;
for j = 1:nContractors
disp(strcat(["Projects assigned to contractor",string(j)," : ", Contractor(j).Project],' '));
disp(strcat(["Resource utilized by contractor ",string(j)," :
",string(ResolurceUtilized(j)),"%"]));
end
disp(strcat(["Total cost : ",string(fopt)]))
disp(“Vivek Mishra”)
Output: