Asked by Saeid Bina
on 11 Nov 2019 at 3:16

Hello everyone, I am new in MATLAB. Please help me find out my problem.

I prepared a code that aimed at finding parameters to fit the data. (data is recorded temperature vs. time)

it is work by changing that three parameters manually but I would like to use fminsearch but always faild!

Thanks in advance.

here is my code (the objective is RMSE):

close all, clear all, clc

load GHE1_6kW_P12_V1.txt

[min,n]=size(GHE1_6kW_P12_V1);

tau = GHE1_6kW_P12_V1(240:end,1);

Tfm_mis = GHE1_6kW_P12_V1(240:end,2);

rho_s = 2533; %[kg/mc]

c_s = 840; %[J/kg.K]

eps = 0.4;

T_0 = 15.96;

rho_w = 1020; %water density [kg/mc]

c_w = 4186;

r_bh= 0.076; %borehole ID/OD=151/165

q = 110.22; %[W/m]

a_s = 0.25;

par=[10,1.7E-6,0.09];

C_0 = rho_s*c_s*(1-eps)+rho_w*c_w*eps;

est = r_bh^2*C_0/4*tau*par(1);

f=(1./est).*exp((-1./est)-((par(2)^2)*c_w^2*r_bh^2*rho_w^2)/16*par(1));

T=cumtrapz(est,f);

dteta= 45*pi/180;

teta =(dteta : dteta : 2*pi);

for j=1:numel(teta)

z(1,j)= q/(4*pi*par(1))*exp((par(2)*rho_w*c_w*r_bh*cos(teta(j)))/2*par(1));

end

g=T*z;

A=mean(g');

B = T_0+A;

Tfm_MLS= B + (q * par(3));

plot(Tfm_MLS,'r')

hold on

plot (Tfm_mis,'g')

RMSE = (sum((Tfm_MLS-Tfm_mis).^2)/length(tau)).^0.5;

Answer by Saeid Bina
on 11 Nov 2019 at 4:10

Yes Par need to be optimaized and while RMSE is minimized. RMSE is the diffrence between calculated and measured data.

I have used loop beacuase that fuction need to be calculated for 8 teta. It is kind a 3D matrix. The data are calculating for timly basis and at each min for 8 teta (degree) and then using Mean for finding average temperature for each min.

by the way if you think is not needed, your idea is respected.

Walter Roberson
on 11 Nov 2019 at 5:48

Vectorizing that loop that calculates z would be more efficient.

Make sure, though, that you use ./ and .* instead of / and * like I show in my code.

Saeid Bina
on 11 Nov 2019 at 6:06

Yes, I have used that one, it results in same. Please help me to optimaize par.

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 1 Comment

## Walter Roberson (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/490348-how-to-use-fminsearch-for-my-code#comment_765863

Sign in to comment.