Curvefit using nonlinearsolve

Com-langmuir model:

qi = Hi * Ci / (1 + Σ(bi * Ci)

i = 2, q and C was obtained from experiment, here is the data df:

Column 1 Column 2 Column 3 Column 4
C_EA C_EC q_EA_303K q_EC_303K
3.46 0.34 7 0.51
5.19 0.51 9.36 0.69
6.93 0.68 11.44 0.94
8.66 0.85 13.96 1.12
10.39 1.02 15.67 1.25
12.12 1.19 19.36 1.51

while H and b are parameters to be fitted. I’ve tried using Lsqfit.jl, But the results obtained were not ideal,how to use NonlinearSolve.jl to fit my parameters?@ChrisRackauckas, thanks in advance.

using NonlinearSolve
function f(du, u, p)
    H_EA, b_EA, b_EC, H_EC = p
   @. du[1] -  H_EA * u[1] / (1 + b_EA * u[1] +b_EC * u[2] )  # du[1] = q_EA_303K, du[2] = q_EC_303K, u[1] = C_EA, u[2] = C_EC
   @. du[2] -  H_EA * u[2] / (1 + b_EA * u[1] +b_EC * u[2] )  
end

Just build a NonlinearLeastSquaresProblem with that f and solve. Did you give that a try? How did it do?

data = [df.C_EA df.C_EC df.q_EA_303K df.q_EC_303K]
6×4 Matrix{Any}:
  3.46  0.34   7     0.51
  5.19  0.51   9.36  0.69
  6.93  0.68  11.44  0.94
  8.66  0.85  13.96  1.12
 10.39  1.02  15.67  1.25
 12.12  1.19  19.36  1.51

function loss_function(p, data)
    Ha, b_EA, b_EC, Hc = p
    res1 = @. Ha * data[:, 1] / (1 + b_EA * data[:, 1] + b_EC * data[:, 2]) - data[:, 3] # data[:, 1] = C_EA, data[:, 2] = C_EC
    res2 = @. Hc * data[:, 2] / (1 + b_EA * data[:, 1] + b_EC * data[:, 2]) - data[:, 4] # data[:, 3] = q_EA, data[:, 4] = q_EC,
    return vcat(res1, res2)
end

p_init = [1.983, 0.0258, 0.2037, 1.53]

nlls_prob = NonlinearLeastSquaresProblem(loss_function, p_init, data)

res = solve(nlls_prob, LevenbergMarquardt(); maxiters = 1000, show_trace = Val(true),
trace_level = TraceWithJacobianConditionNumber(25))



Algorithm: LevenbergMarquardt(
   trustregion = LevenbergMarquardtTrustRegion(β_uphill = 1.0),
   descent = GeodesicAcceleration(descent = DampedNewtonDescent(initial_damping = 1.0, damping_fn = LevenbergMarquardtDampingFunction()), finite_diff_step_geodesic = 0.1, α = 0.75)
)

----     -------------        -----------          -------             
Iter     f(u) 2-norm          Step 2-norm          cond(J)             
----     -------------        -----------          -------             
0        4.94241279e+00       1.38455821e-309      Inf                 
1        1.82069420e+00       1.32719843e-01       3.81961686e+04      
26       1.06073162e+00       0.00000000e+00       4.10418461e+04      
51       1.06073162e+00       0.00000000e+00       4.10418461e+04      
76       1.06073162e+00       0.00000000e+00       4.10418461e+04      
101      1.06073162e+00       2.11244819e-15       4.10418461e+04      
126      1.06073162e+00       0.00000000e+00       4.10418461e+04      
151      1.06073162e+00       1.03030808e-15       4.10418461e+04      
Final    1.06073162e+00      
----------------------      
retcode: Stalled
u: 4-element Vector{Float64}:
    1.959101723669487
   13.582173936040325
 -138.1163224956984
    1.5711765683245895

Same answer, seems like no problem