Info

# Minimize variable by changing 3 coefficients

1 view (last 30 days)
Benjamin on 24 Oct 2019
Closed: MATLAB Answer Bot on 20 Aug 2021
I have a complicated code with several functions. But ultimately, I want to change 3 variables in one function to minimize a different variable. I can supply the entire code if needed. But how do I change B1, B2, and B3 to minimize the variable "diff"
diff = xma - xm_data;
function C_function = C(rpf)
B1=1.90322581;
B2=5.16298862510519;
B3=-6.54826154496761;
C_function = (1 + B1.*rpf.^1 + B2.*rpf.^2 + B3.*rpf.^3)./(3.*(1-rpf));
end
In case anyone wants the whole code, here it is. I just want the code to vary B1, B2, and B3 to get the best fit to the data. I am trying to accomplish this by just minimzing the difference between the result and the data with the "diff" variable. So, I want the code to keep iterating until "diff" is as small as it can be by changing B1, B2, and B3.
close all;
clear; clc;
rpf_data = mat(:,1);
xm_data = mat(:,2);
xma=[];
rpfa=[];
for rpf = 0.000:0.01:0.800
x = 1:0.005:2;
f = (sqrt(2)*pi/3).*g(x,rpf).*g_0(rpf)./x;
Intf = cumtrapz(x,f);
ix = find(Intf > 1, 1, 'first');
ix_r = x(:,ix);
ix_r = ix_r';
xma = [xma,ix_r];
rpfa = [rpfa,rpf];
x = 1:0.00001:ix_r;
end
figure()
plot(rpfa,xma,'ro','MarkerSize',1)
hold on;
plot(rpf_data,xm_data,'ko','MarkerSize',1);
grid on;
xma = xma';
diff = xma - xm_data;
diff = sum(diff)
function rdf = g(x,rpf)
rdf = x./((1+C(rpf).*(x-1)).^3)+(1-x./((1+1.0/3.0.*(x-1)).^3));
end
function rdf_contact = g_0(rpf)
A1 = -0.419354838709677;
A2 = 0.581165452653486;
A3 = 0.643876195271502;
A4 = 0.657898291526944;
A10 = 0.651980607965746;
A14 = -2.6497739490251;
eta_c = 0.6452;
rdf_contact = (3./(eta_c*(1-rpf))+(1./eta_c)*((1).*A1.*rpf + (2).*A2.*rpf + (3).*A3.*rpf.^2 + (4).*A4.*rpf.^3 + (10).*A10.*rpf.^9 + (14).*A14.*rpf.^13))/4;
end
function C_function = C(rpf)
B(1)=1.90322581;
B(2)=5.16298862510519;
B(3)=-7.54826154496761;
C_function = (1 + B(1).*rpf.^1 + B(2).*rpf.^2 + B(3).*rpf.^3)./(3.*(1-rpf));
end