How can we run for loop in order to get several fitting parameters?
4 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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
0 Commenti
Risposte (1)
Ayush Aniket
il 12 Mar 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.
0 Commenti
Vedere anche
Categorie
Scopri di più su Linear and Nonlinear Regression in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!