-1

If we have k=1:n, and then we put k in a function so we get fk=f(tk) for some values of tk, when I apply the program I get just fk for each value of k, it is not named by f1, f2. Here is the statements

for k=1:n
t=linspace(0,0.25)
tk=t.*(k-1)/n
f=inline('x.^0.2')
fk=f(tk)
Arastas
  • 2,329

1 Answers1

0

Normally, it is not a good idea to create new variables in a loop. It is better to have an array or a cell array. However, if you really need it, then one possible solution is:

n = 5;
t = linspace(0,0.25);
f = @(x) x.^0.2;
for k = 1:n
    tk = t.*(k-1)/n;
    s = ['f' num2str(k) ' = f(tk);'];
    evalin('caller',s);
end

I avoide inline because it will be removed.

Arastas
  • 2,329
  • Thank you for your reply, it is very useful. But I didn't touch the use of (evalin)?? what if I want to add another index j and calculate fj-fk? Is it just like this or the names doesn't match the values of the function?? – Hawraa Abbas Jul 09 '19 at 18:30
  • @HawraaAbbas evalin just executes a string. So I create the string f1=f(tk);, where 1 in f1 is created based on the current index k. Please do not forget to mark an answer as a solution if it is. – Arastas Jul 10 '19 at 09:06