Error: Custom function fitting in MATLAB
Show older comments
Hey guys, I would like to know why I got this error and how can I fix it. Can I please have your guys comments on this code:
% Load the experimental data and assgin the data to each parameter
data2fit2 = xlsread('data2fit2.xlsx');
ST = data2fit2(:,1);
theta_bar = data2fit2(:,2);
f_exp = data2fit2(:,3);
% Initial Guess
par0 = [0.417257,0.04132,0.001177,0.00961,1.946234];
pars = zeros(size(par0));
% Get the estimate parameters to have best fit to measurement data
par = fminsearch(@(par) err_fcn(pars,...
data2fit2,...
ones(size(data2fit2)),...
@(s,t,p) epsilon_f_of_sigma(s,t,p),...
ST,...
theta_bar),...
par0);
% Use optimized parameters to calculate the estimated epsilon_f by HC model
epsilon_f_modeled = epsilon_f_of_sigma(ST, theta_bar, par);
% Plot fracture criterion
figure;
plot(ST, f_exp, 'o', ST, epsilon_f_modeled, '-')
xlabel('Stress Triaxiality [-]')
% ylabel('Lode Angle Parameter [-]')
ylabel('Equivalent Plastic Strain at Fracture [-]')
function epsilon_f = epsilon_f_of_sigma(ST, theta_bar, pars)
b0 = pars(1);
gamma = pars(2);
c = pars(3);
epsilon_p = 0.0005;
epsilon_0 = 0.0005;
n = pars(4);
a = pars(5);
% Determine strain rate effect on parameter b
if epsilon_p < epsilon_0
b = b0;
else
b = b0*(1 +gamma*log(epsilon_p/epsilon_0));
end
% Define Lode-dependent functions
f1 = (2/3)*cos((pi/6)*(1-theta_bar));
f2 = (2/3)*cos((pi/6)*(3+theta_bar));
f3 = -(2/3)*cos((pi/6)*(1+theta_bar));
% Define each term
Strain_rate_term = b*(1+c).^(1./n);
Lode_dependent_term = ((1/2).*((f1-f2).^a+(f2-f3).^a+(f1-f3).^a)).^(1./a);
Triaxiality_term = c.*(2*ST+f1+f3);
% Final expression for HC model
epsilon_f = Strain_rate_term.* (Lode_dependent_term + Triaxiality_term).^(-1./n);
end
% Define error function
function err = err_fcn(pars,data2fit2,weights4scaling,model_fcn,ST,theta_bar)
model_data = model_fcn(ST,theta_bar,pars);
err = sum((data2fit2(:) - model_data(:)).^2.*weights4scaling);
end
Accepted Answer
More Answers (0)
Categories
Find more on Get Started with Curve Fitting Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
