error when using lsqnonlin to function containing fzero function

2 Ansichten (letzte 30 Tage)
I want to use lsqnonlin analysis to fit a model to two sets of data and obtain values of 4 variables. The function i created contains fzero function which only allows scalar values and i finally got the error 'operands to the || and && operators ust be convertible to logical scalar values. ' So is there any other method that can replace lsqnonlin to solve my problem? Or any corrections, changes to be made in my code to solve this with lsqnonlin? Thanks! Here's my function file and code.
function Result = myfunc1(a,p,G,m,fraction)
totalconcentration = 0.0000291;
n = 2;
K = exp(-((G + m * fraction)./(8.314*298)));
g_lb = 0;
g_ub = 0.9999;
g =[g_lb g_ub];
monomerconcentration = fzero(@denaturationfun,g);
function y = denaturationfun(x)
y = a.^(-1).*(((a.*x).^(n+1)).*(n.*a.*x-n-1)/((a.*x-1).^2)+a.*x/((a.*x-1).^2))-a.^(n-1).*((x^(n+1)).*(n.*x-n-1)/((x-1).^2))-K.*totalconcentration;
end
Result = p * (1- (monomerconcentration / ( K * totalconcentration )));
end
clc;
clear;
close all;
fractiondata = [0.1505 0.1546 0.1587 0.1628 0.1668 0.1708 0.1748 0.1787 0.1825 0.1864 0.1902 0.194 0.1977 0.2014 0.205 0.2087 0.2123];
DegreeofAggdata = [1 0.9087 0.8658 0.83453 0.79569 0.67979 0.62031 0.53043 0.39722 0.25888 0.15171 0.04759 0.00109 3.20E-04 0.00144 7.77E-04 0];
fun = @(x)DegreeofAggdata - myfunc1(x(1),x(2),x(3),x(4),fractiondata);
x0 = [0.01,0.9,-50000,100000];
lb = [0.00001,0.9,-100000,20000];
ub = [1,1.1,-10000,130000];
x = lsqnonlin(fun,x0,lb,ub);
Error using fzero (line 236)
Function values must be scalar.

Error in solution>myfunc1 (line 8)
monomerconcentration = fzero(@denaturationfun,g);

Error in solution>@(x)DegreeofAggdata-myfunc1(x(1),x(2),x(3),x(4),fractiondata) (line 19)
fun = @(x)DegreeofAggdata - myfunc1(x(1),x(2),x(3),x(4),fractiondata);

Error in lsqnonlin (line 221)
initVals.F = feval(funfcn{3},xCurrent,varargin{:});

Caused by:
Failure in initial objective function evaluation. LSQNONLIN cannot continue.

Akzeptierte Antwort

Torsten
Torsten am 13 Sep. 2024
Verschoben: Torsten am 13 Sep. 2024
"K" is a vector because "fraction" is a vector. Thus the "y" you compute in "denaturationfun" is a vector. But "fzero" cannot solve systems of nonlinear equations.
So either use "fsolve" or call "fzero" in a loop over all K-values.

Weitere Antworten (0)

Produkte


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by