Clear Filters
Clear Filters

已知自变量、因变量与函数,优化出各参数的值

5 views (last 30 days)
浩天
浩天 on 10 Apr 2024
Answered: Ayush on 18 Apr 2024
已知一元函数y1=(ax^2+bx+c)/(dx+e),已知自变量x的100个不同值与在不同x下对应的经实验测量得到的结果y2(y1为y2在同一自变量x下的理论值,二者存在一定误差),如何拟合出abcde各参数的最优解,使得y1与y2的误差最小

Answers (1)

Ayush
Ayush on 18 Apr 2024
Hello 浩天,
I will be answering your query in English, since I am not proficient in Chinese. Based on your description, it seems you want to minimize the error between the theoretical value and the experiment value based on the equation containing an independent variable.
A common approach for such error minimization is to leverage the MATLAB’s Optimization Toolbox by the method of non-linear squares fitting. Here is the detailed overview of the possible steps:
1. The format or data type of the independent variable (x) and the experimentally measured (y2) must be same, typically vectors. Here is an example code for your reference:
x_data = [x1, x2, x3, ..., x100]; % Your x data
y2_data = [y21, y22, y23, ..., y2100]; % Your y2 data (experimental)
2. Next, you would need to define the equation mentioned in the above problem into MATLAB. This can be done with anonymous function in MATLAB as shown below:
modelFcn = @(p, x) (p(1)*x.^2 + p(2)*x + p(3)) ./ (p(4)*x + p(5));
% p is a vector of the parameters [a,b,c,d,e] of your equation
To learn more about anonymous functions, kindly refer to the link below:
3. For the defined parameters of the equation, you can make an initial guess which might help in the convergence of the optimization algorithm.
initialGuess = [1, 1, 1, 1, 1]; % Example guess
4. Now, you can utilize the “lsqcurvefit” to fit your equation to the data to minimize the sum of squares of the deviations i.e. errors. You can refer to the below code to know how to use the function in the context of your problem:
% Define options for lsqcurvefit (optional)
options = optimoptions('lsqcurvefit','Display','iter','Algorithm','trust-region-reflective');
% Perform the fitting
[p_est,~,residual,exitflag,output] = lsqcurvefit(modelFcn, initialGuess, x_data, y2_data, [], [], options);
% p_est will contain your fitted parameters [a, b, c, d, e]
To learn more about "lsqcurvefit" function, kindly refer to the link below:

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!