Create dynamic functions and variable in a loop

Hi,
I try to implement a routine to solve a set of equations by using the fsolve functions. The code is of the form
optimFun=@(T)[harmonicOscilator(data,Numb,T(1),0)-c(1)-(T(1)+T(2)+T(3))-c(2)-c(3)-c(4);
fun(T(2))-fun(T(1))+c(2)+T(1);
fun(T(3))-fun(T(2)),0)+c(3)+T(2)];
options=optimset('Display','iter');
[T,fval] = fsolve(optimFun,T0,options);
where the c's are constant which are defined by an external function call and fun(T) is a function call were the function itself depends on T. For this example the code solve the problem. However, I need to be able to solve these equations for a varying number of variables T(1),T(2),T(3),... and a varying number of coupled equations. For example the next bigger set of equations would be
optimFun=@(T)[harmonicOscilator(data,Numb,T(1),0)-c(1)-(T(1)+T(2)+T(3)+T(4))-c(2)-c(3)-c(4)-c(5);
fun(T(2))-fun(T(1))+c(2)+T(1);
fun(T(3))-fun(T(2)),0)+c(3)+T(2);
fun(T(4))-fun(T(3)),0)+c(4)+T(3)];
Is there any way to define the function optimFun and the corresponding variables dynamically, so I do not have to vary the set of equations by hand? I tried using a loop and other things, however, it did not work in the end.
Looking forward to hearing from you
All the best
Sven

Answers (0)

Products

Asked:

on 24 May 2013

Community Treasure Hunt

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

Start Hunting!