Hello. I am having trouble generating an equation to estimate parameters given a set of data for B and T. I am about to use lsqcurve fitting but for some reason i could not make the code run.
The equation should be in the form of :
See below:
T = [120,210,400,540,650];
B = [-344,-157,-17.5,-0.85,10.5];
fun = @(constant,T)(int(((2.*pi.*(constant(1)).^3)./3).*(1-exp((-4.*constant(2)./T)*((1./y.^4)-(1./y.^2)))),y,0,10));
T0 = [110,200]; %I just entered a random number, but don't know what this is for
T = lsqcurvefit(fun,T0,T,B);
The error I am getting is:
Unrecognized function or variable 'y'.
Error in
samplesept19>@(constant,T)(int(((2.*pi.*(constant(1)).^3)./3).*(1-exp((-4.*constant(2)./T)*((1./y.^4)-(1./y.^2)))),y,0,10))
(line 9)
fun =
@(constant,T)(int(((2.*pi.*(constant(1)).^3)./3).*(1-exp((-4.*constant(2)./T)*((1./y.^4)-(1./y.^2)))),y,0,10));
Error in lsqcurvefit (line 225)
initVals.F = feval(funfcn_x_xdata{3},xCurrent,XDATA,varargin{:});
Error in samplesept19 (line 12)
T = lsqcurvefit(fun,T0,T,B);
Caused by:
Failure in initial objective function evaluation. LSQCURVEFIT cannot continue.
Hoping for answers and help. :) Thank you so much!

 채택된 답변

Walter Roberson
Walter Roberson 2021년 9월 19일

0 개 추천

format long g
T = [120,210,400,540,650];
B = [-344,-157,-17.5,-0.85,10.5];
syms t y
syms constant [1 2]
expr = ((2.*pi.*(constant(1)).^3)./3).*(1-exp((-4.*constant(2)./t)*((1./y.^4)-(1./y.^2))))
expr = 
intexpr = int(expr, y, 0, 10)
intexpr = 
RHS = 2/3.*pi.*(constant(1)).^3 .* intexpr;
fun = matlabFunction(RHS, 'vars', {constant, t})
fun = function_handle with value:
@(in1,t)in1(:,1).^3.*pi.*integral(@(y)in1(:,1).^3.*pi.*(exp((in1(:,2).*(1.0./y.^2-1.0./y.^4).*4.0)./t)-1.0).*(-2.0./3.0),0.0,1.0e+1).*(2.0./3.0)
obj = @(P,T) arrayfun(@(t) fun(P,t), T)
obj = function_handle with value:
@(P,T)arrayfun(@(t)fun(P,t),T)
P0 = [110,200]; %I just entered a random number, but don't know what this is for
P = lsqcurvefit(obj, P0, T(:), B(:))
Local minimum possible. lsqcurvefit stopped because the final change in the sum of squares relative to its initial value is less than the value of the function tolerance.
P = 1×2
1.6038792022093 184.707909161448
B2_predicted = obj(P,T(:));
plot(T, B, 'k*', T, B2_predicted, 'b-+')
legend({'original B', 'predicted B'}, 'location', 'best')

댓글 수: 3

lvenG
lvenG 2021년 9월 20일
Hello Sir @Walter Roberson. Thank you so much for the help. Is there any way that this code can calculate the value of constant(1) and constant(2)? When I run this, it just returns the value constant and did not calculate it. Please enlighten me. Thank you.
Walter Roberson
Walter Roberson 2021년 9월 20일
P(1) is constant(1) and P(2) is constant(2)
lvenG
lvenG 2021년 9월 20일
@Walter Roberson, thank you so much Sir. This worked.... Really, thanks for the help and enlightenment... Hope you are having a great day! Stay safe.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

태그

질문:

2021년 9월 19일

편집:

2021년 9월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by