Message Boards Message Boards

Can I create a function by assigning list elements?

Posted 10 years ago

Hello, I have two lists containing numerical data. I want to create a function by assigning all of the elements of one list as arguments of this function and respectively all of the elements of the second list as values of that function. Let's say list A is {0,4,7,8} and list B is {1,4,5,3}. I want a function that for x=0 has y=1, for x=4 y=4, for x=7 y=5 and for x=8 y=3. For all other x the function is not defined. How can it be done?

For a broader perspective: those two lists are my experimental data. I am later using that function to calculate mean square error between experimental data and ideal model. Then I use NMinimize function to obtain parameters from the ideal model that fit my experimental data best. This is why I need those lists as a function.Let's say Texp is my function made from lists:

(*Definition of function to be optimized*)
 Ksi2=Sum[(Texp[x]-Model)^2,{x,x_max}];
(*optimizing function*)
 opt=NMinimize[Re[Ksi2],{parameter_1,parameter_2,parameter_3}];
2 Replies

Thank you.

In[1]:= list1 = Range[5];

In[5]:= list2 = RandomReal[{0, 1}, 5]

Out[5]= {0.616207, 0.0909475, 0.963306, 0.63963, 0.714089}

In[6]:= Do[f[list1[[i]]] = list2[[i]], {i, 5}]

In[7]:= ?f

Global`f

f[1]=0.616207

f[2]=0.0909475

f[3]=0.963306

f[4]=0.63963

f[5]=0.714089
POSTED BY: Frank Kampas
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract