# MATLAB fminsearch

1. Dec 22, 2008

### Saldita

Hi,
I'm trying to vary the parameter of a function and optimizing it by applying fminsearch.
though I don't seem to get very far.
I would really appreciate any help. Thanx!!!

This is the matlab code
%Optimierung9

function Kurvenfit
%Parameter, Data, Prep
KL=[1 200 400]; Ks = num2str(KL'); % some K-values
rL=[0.1 0.2 0.5]; rs = num2str(rL'); % some r-values
y0L = [0.5 0.1 2]; y0s =num2str(y0L'); % some y0-values
cL = 'brgmk'; % colours

tdat =[3 5 10 15 20 30 40 50 60 70 80 100]; %given Data
y = [0.5 1 2 7 15 30 70 130 170 230 248 252];
t= [0:0.1:100]; %t-Axis

%(1) vary K_________________________________

subplot (4,1,1); hold on; % (1) K varying
leg = [];
plot (tdat, y,'ko') %add data as circles
legend(leg,4)
xlabel('Zeit in Tagen')
ylabel('Hoehe (cm)')
for i = 1:length(KL)
K = KL(i); % variable Parameter
r = rL(2); % fester Parameter
y0 = y0L(2);
[t,y]=ode45(@f,[0 100],[],y0(2),r(2),K(i));
leg = [leg;['K = ',Ks(i,:)]]; % Legende aufbauen
plot(t,ydot,cL(i))
end

function ydot=f(t,y,r,K)
ydot=r*((1-y/K).*y);

#this is how far I get and then matlab says:

??? Index exceeds matrix dimensions.
Error in ==> Optimierung9 at 28
[t,y]=ode45(@f,[0 100],[],y0(2),r(2),K(i));
%
% (2)vary r_______________________________
%
% subplot (4,1,2); hold on
% leg = [];[/COLOR]% for i = 1;length(rL)
% [t,y]=ode45(@f,[0 12],[],y0(2),r(i),K(2));
% plot(t,ydot,cL(i))
% leg = [leg;['r = ',rs(i,:)]];
% end
% plot(t,ydot,'ko')
% legend(leg,4)
%
% (3)vary y0____________________________
% subplot (4,1,2); hold on
% leg = [];
% for i = 1;length(rL)
% [t,y]=ode45(@f,[0 12],[],y0(i),r(2),K(2));
% plot(t,ydot,cL(i))
% leg = [leg;['r = ',rs(i,:)]];
% end
% plot(t,ydot,'ko')
% legend(leg,4)
%
% (4) optimize Parameter______________________
% subplot(3,1,3); hold on
% par0([r(2),K(2),y0(2)]) % start parameter
% plot(t,ydot(t,par0),'b:') % function with start parameter
%
% par = fminsearch(@Derqua,par0,[],t,y); % optmizing
%
% plot (t,ydot(t,par),'r') % plot otimized function
% plot(t,ydot,'ko') % add data to plot
% legend('Start','optimal','Daten',4) % Legende
%
% ___
% function Q = Derqua(par,t,y)
% ydot=ydot(t,par);
% Q=sum((ydot-ydot).^2);
%
% function ydot=ydot(t,par);
% ydot=par(1)*t./(t+par(2));[/COLOR[/COLOR]]