How to write a file function for multiple regression?

Problem Statement: Based on the data in the table below and multiple linear regression develop the following two models for the diffusion coefficient of N2O in aqueous MDEA as a function of Temperature (T) and weight fraction MDEA(X). As part of your solution write a function named Mregress that will perform multiple linear regression hat can be called from the main script file where the data is defined (i.e the data is passed to your function). Solve for constants in equation: D=a0+a1T+a2X. (See data listed in 'My main script')
Issue: I do not know how to write the syntax of the Mregress function to where it will work for an different problem of multiple regression.
My Mregress file code:
function a= fn_Mregress (x,y,m,n)
%Coefficient Matrix (C)
for j=1:m
for k=1:m
sum=0;
for i=1:n
sum=sum+x(j,i)*x(k,i);
end
c(j,k)=sum;
end
end
% Constant Vector (b)
for j=1:m
sum=0;
for i=1:n
sum=sum+x(j,i)*y(i);
end
b(j)=sum;
end
a=c\b';
My main script:
m=3;
n=9;
D=[0.823;0.639;0.430;0.973;0.751;0.506;1.032;0.824;0.561];
MDEA=[0.3;0.4;0.5;0.3;0.4;0.5;0.3;0.4;0.5;];
T=[20;20;20;25;25;25;30;30;30];
for j=1:n
y(j)=D(j);
x(1,j)=1;
x(2,j)=T(j);
x(3,j)=MDEA(j);
end
x=x(m,j);
y=y(j);
fn_Mregress(x,y,3,9);
fprintf ('D = %.4f + %.4fT + %.4fX\n',a(1),a(2),a(3))
Thank you for any help!

回答(0 个)

类别

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by