curve fit toolbox help system ODE and excel?
조회 수: 1 (최근 30일)
이전 댓글 표시
Hi, I am trying to curve fit using the toolbox to a function which is a system of ODE. my data corresponds to one equation of the function. how can i specify this? Also, the tutorial seems to say you should put your data into the command line, but mine is in excel, is there some easier way? Thanks!
댓글 수: 0
채택된 답변
Matt Tearle
2014년 10월 23일
If I interpret your problem correctly, you have a system of ODEs with a parameter -- y' = f(t,y;c) -- and an Excel spreadsheet that has data for t and y3 (or whatever -- one of the components of y). You're trying to do a curve fit to determine c.
That's fun!
Here's an example that does that with the classic mass-spring system. I'm making some t and y data; you could read this from Excel with xlsread. You need to do this only once at the beginning, then you have the data to fit to.
% ---------------------------------------------------------------
% Make some data -- replace all this with a call to XLSREAD
m = 0.1*rand;
k = 4*rand;
w = 0.5*sqrt(4*k-m*m);
t = linspace(0,2*pi)';
y = exp(m*t/2).*cos(w*t) + 0.02*randn(size(t));
% ---------------------------------------------------------------
% Do curvefitting
cfit = lsqcurvefit(@myode,rand(2,1),t,y)
% See results
plot(t,y,'o',t,myode(cfit,t))
And here's the curve-fitting function:
function y = myode(c,x)
% Define ODE system with parameter c
f = @(t,y,c) [y(2);-c(1)*y(1)+c(2)*y(2)];
% Solve ODE system with given initial condition
[~,z] = ode45(@(t,y) f(t,y,c),x,[1;0]);
% Extract desired solution component
y = z(:,1);
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Global or Multiple Starting Point Search에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!