![How to do a nonlinear fit using least squares - 2019 09 14.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/238352/How%20to%20do%20a%20nonlinear%20fit%20using%20least%20squares%20-%202019%2009%2014.png)
How to do a nonlinear fit using least squares
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Andrew
el 14 de Sept. de 2019
Comentada: Star Strider
el 15 de Sept. de 2019
I have a set of data points giving me the values for the second virial coefficient,
for various values of T, of the virial expansion which is an equation that corrects the ideal gas law for empirical deviations:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/238353/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/238354/image.png)
I'm trying to do a least squares fit to determine how well the van der Waals equation predicts
using MATLAB.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/238353/image.png)
The equation you derive for B using van der Waals' equation ends up being:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/238355/image.png)
where a and b are unknown constants I need to determine and R is just the ideal gas constant.
Now my data points for true
values are here:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/238353/image.png)
B = [-160 -35 -4.2 9 16.9 21.3]*10^-6 ;
T = [100 200 300 400 500 600]; (kelvin)
How can I write some code which will use the least squares method to generate estimates for a and b using the given data points?
My problem is that this is essentially a
equation which I'm not sure how to represent in MATLAB.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/238356/image.png)
I can only find options for quadratic, cubic, polynomial fit etc.
0 comentarios
Respuesta aceptada
Star Strider
el 15 de Sept. de 2019
Editada: Star Strider
el 15 de Sept. de 2019
This is actually a linear problem, so a linear approximation will estimate the parameters correctly:
R = 8.314462; % J K^−1 mol
B = [-160 -35 -4.2 9 16.9 21.3];
T = [100 200 300 400 500 600];
ab = [ones(size(B(:))) - 1./(R*T(:))] \ B(:); % Linear Approximation Parameter Estimation
Bfit = [ones(size(B(:))) - 1./(R*T(:))] * ab; % Linear Fit
figure
plot(T, B, 'pg')
hold on
plot(T, Bfit,'-r')
hold off
grid
xlabel('T')
ylabel('B')
legend('Data','Linear Least-Squares Fit', 'Location','E')
The parameters are:
a = 64.2001320479734
b = 182307.574287957
EDIT —
Added plot figure:
![How to do a nonlinear fit using least squares - 2019 09 14.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/238352/How%20to%20do%20a%20nonlinear%20fit%20using%20least%20squares%20-%202019%2009%2014.png)
8 comentarios
Más respuestas (1)
Jackson Burns
el 15 de Sept. de 2019
Attached is code generated by cftool using a custom equation. Hope this helps!
5 comentarios
Jackson Burns
el 15 de Sept. de 2019
If you're making this call in a function, it will tell you that result is unused if you don't later on reference result or return it from the function.
Ver también
Categorías
Más información sobre Interpolation en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!