How can we run for loop in order to get several fitting parameters?

1 visualización (últimos 30 días)
Farhan Ashraf
Farhan Ashraf el 11 de Abr. de 2018
Respondida: Ayush Aniket el 12 de Mzo. de 2025
Hello, I am the fitting experimental data with custom equation and I want to do it for several numbers to check uncertainty. The code is fitting value for one number but not in the loop. Can you help me regarding this? when I introduce (i) with ft in the code shown below, the following error occurs: double objects can't be assigned to using subscripts
if
function [fitresult, gof] = createFit(temp, yieldstress)
temp=[1 2 3 4 5];
yieldstress=[10 20 30 40 50];
nsample=100;
for i=1:nsample
DD=1e12+2e12*rand(1);
self_coff=0.08+0.04*rand(1);
line_energy=0.5+0.5*rand(1);
C_F=0.32+0.06*rand(1);
threshold_stress=(25.9e9*2.86e-10.*sqrt(DD).*((0.5.*line_energy)+sqrt(self_coff)))./(1e6);
ave_strainrate=2.86e-10*1e12.*sqrt(DD);
[xData, yData] = prepareCurveData( temp, yieldstress );
% Set up fittype and options.
ft(i) = fittype( strcat('(',num2str(threshold_stress),'+((0.89.*s).*((1-(((-8.617e-5.*x)./F).*',num2str(log(6.667e-4./ave_strainrate)),').^(1/q))^(1/p))))./',num2str(C_F)), 'independent', 'x', 'dependent', 'y');
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
opts.Lower = [1.1 0.667 1.5 3];
opts.StartPoint = [1.8 0.667 1.5 4];
opts.TolX = 1;
opts.Upper = [3 1 1.5 20];
end
% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft, opts );
end

Respuestas (1)

Ayush Aniket
Ayush Aniket el 12 de Mzo. de 2025
The error occurs because you are trying to store fittype objects in an array using subscripts. In MATLAB, arrays of objects like fittype cannot be indexed in the same way as numeric arrays. Instead, you should use a cell array to store these objects as show below:
function [fitresult, gof] = createFit(temp, yieldstress)
temp = [1 2 3 4 5];
yieldstress = [10 20 30 40 50];
nsample = 100;
% Initialize cell array to store fittype objects
ft = cell(nsample, 1);
for i = 1:nsample
DD = 1e12 + 2e12 * rand(1);
self_coff = 0.08 + 0.04 * rand(1);
line_energy = 0.5 + 0.5 * rand(1);
C_F = 0.32 + 0.06 * rand(1);
threshold_stress = (25.9e9 * 2.86e-10 * sqrt(DD) * ((0.5 * line_energy) + sqrt(self_coff))) / (1e6);
ave_strainrate = 2.86e-10 * 1e12 * sqrt(DD);
[xData, yData] = prepareCurveData(temp, yieldstress);
% Create fittype object and store it in the cell array
ft{i} = fittype(strcat('(', num2str(threshold_stress), '+((0.89.*s).*((1-(((-8.617e-5.*x)./F).*', ...
num2str(log(6.667e-4 / ave_strainrate)), ').^(1/q))^(1/p))))./', num2str(C_F)), ...
'independent', 'x', 'dependent', 'y');
opts = fitoptions('Method', 'NonlinearLeastSquares');
opts.Display = 'Off';
opts.Lower = [1.1 0.667 1.5 3];
opts.StartPoint = [1.8 0.667 1.5 4];
opts.TolX = 1;
opts.Upper = [3 1 1.5 20];
[fitresult{i}, gof{i}] = fit(xData, yData, ft{i}, opts);
end
end
This will allow you to perform multiple fits with different parameters in a loop.

Categorías

Más información sobre Linear and Nonlinear Regression en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by