How to do a nonlinear fit using least squares
이전 댓글 표시
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:
for various values of T, of the virial expansion which is an equation that corrects the ideal gas law for empirical deviations:I'm trying to do a least squares fit to determine how well the van der Waals equation predicts
using MATLAB.
using MATLAB.The equation you derive for B using van der Waals' equation ends up being:

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:
values are here: 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.
equation which I'm not sure how to represent in MATLAB.I can only find options for quadratic, cubic, polynomial fit etc.
채택된 답변
추가 답변 (1개)
Jackson Burns
2019년 9월 15일
1 개 추천
Attached is code generated by cftool using a custom equation. Hope this helps!
댓글 수: 5
Andrew
2019년 9월 15일
Jackson Burns
2019년 9월 15일
편집: Jackson Burns
2019년 9월 15일
If you use the function like this:
[result,~] = createFit(T,B)
result contains the coefficients of the function a and b.
Glad it helped!
Andrew
2019년 9월 15일
Jackson Burns
2019년 9월 15일
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.
Andrew
2019년 9월 15일
카테고리
도움말 센터 및 File Exchange에서 Least Squares에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
