Lsqcurvefit on function depending on syms

조회 수: 6 (최근 30일)
Gustav
Gustav 2012년 3월 12일
Hi.
I'm using the int command to integrate a huge expression, using syms. The expression is saved in a variable "funk". In funk is a constant that I need to determine by fitting "funk" to my experimental data. For this I have tried to use lsqcurvefit, but I can't get this to work by simple putting "funk" in the expression where the function is suppose to be written. This is how it looks:
funk=huge expression with syms x
xdata=..
ydata=..
lsqcurvefit(@(x,xdata) funk, [some value] ,xdata,ydata)
How can I do this? Please help!

채택된 답변

Seth DeLand
Seth DeLand 2012년 3월 12일
Hi Gustav,
You'll first need to use the matlabFunction command to convert the symbolic equation into a MATLAB function handle or file: http://www.mathworks.com/help/toolbox/symbolic/matlabfunction.html
There's an example here that should help: (see Figure 2) http://www.mathworks.com/tagteam/62669_91801v00_SymbolicGradient_Hessian_final.pdf

추가 답변 (2개)

Gustav
Gustav 2012년 3월 12일
Ok, thanks!
Still, don't get it work. Heres an example (if I can get this to work it will solve my real problem):
xdata = [2;4;6]; %data
ydata = 3*sin([5;4;6])+6; %data
syms d t
uttryck=d*cos(t);
funk=matlabFunction(uttryck);
t=xdata;
d = lsqcurvefit(@(d,t) funk,7,t,ydata)
But I get the error message "Function value and YDATA sizes are incommensurate."
Simply, I'm trying to optimize d to fit the experimental data. Whats wrong?
  댓글 수: 1
Seth DeLand
Seth DeLand 2012년 3월 13일
Because funk is already a function handle (that's what matlabFunction created), you don't need to use the @(d,t) syntax when passing it to lsqcurvefit. You can just pass funk:
d = lsqcurvefit(funk,7,t,ydata)

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


Gustav
Gustav 2012년 3월 16일
Nice! Works perfectly! Thank you so much!

카테고리

Help CenterFile Exchange에서 Conversion Between Symbolic and Numeric에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by