Info

This question is closed. Reopen it to edit or answer.

update my_fun for lsqcurvefit during runtime

1 view (last 30 days)
Eide
Eide on 14 Nov 2012
Closed: MATLAB Answer Bot on 20 Aug 2021
Hi,
I am working on a parameter estimation problem using the lsqcurvefit function. During runtime I want to change the function 'my_test_func' that is used in the fitting. So inside my matlab script I do the following:
for jj = 1:1:10
% create r
r = r.';
%%create my fun %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ni = (jj * 2) + 1;
name = sprintf('my_test_func');
ending = sprintf('.m');
filename = strcat(name,ending);
fid = fopen(filename,'w');
insidename = strcat('function [F] = ',name,'(x,xdata)');
fprintf (fid,insidename);
fprintf (fid,' \n');
final_wort = ' F = ';
for i = 1:ni
if (i == 1)
wort = sprintf(' (x(1) - x(2) * log(xdata(:,%u)/0.8))',i);
elseif (i > 1) && (i < ni)
wort = sprintf(' + (x(1) - x(2) * log(xdata(:,%u)/0.8))',i);
elseif (i == ni)
wort = sprintf(' + (x(1) - x(2) * log(xdata(:,%u)/0.8));',i);
end
final_wort = strcat(final_wort,wort);
end
fprintf(fid,'%s\n',final_wort);
fprintf (fid,' \n');
fprintf (fid,'return; \n');
fclose(fid);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%fitting input
% reset
guess140 = 0.0;
xdata = 0.0;
ydata = 0.0;
xdata = [r(:,1:(jj*2)+1)];
ydata = [observed_data];
% do the trick
guess140(jj,1:2) = lsqcurvefit(@my_test_func,[350 8], xdata, ydata);
%disp([num2str(guess140),' XXXXX ',num2str(jj)])
end % jj
My problem is that lsqcurvefit is not using the current 'my_test_func' but the very first one. How is it done correctly? Is there a way to update 'my_test_func' during runtime (within a loop)? Thank you in advance.
Eide

Answers (0)

This question is closed.

Community Treasure Hunt

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

Start Hunting!