build functions with str2func command in matlab -


i want build series of different function matlab , integrate , differentiate results. mathwork says output of str2func can't access variable or bot used other function. 1 me problem?

i want create these function:

f1= @(x,l) x.*(l-x);                                       f2= @(x,l) x.^2.*(l-x).^2.*(l/2-x).^2;                    f3= @(x,l) x.^3.*(l-x).^3;                                 f4= @(x,l) x.^4.*(l-x).^6.*(l/2-x).^4;                   f5= @(x,l) x.^5.*(l-x).^5;                               f6= @(x,l) x.^6.*(l-x).^6.*(l/2-x).^6;                     f7= @(x,l) x.^7.*(l-x).^7;                                 f8= @(x,l) x.^8.*(l-x).^8.*(l/2-x).^8;                    f9= @(x,l) x.^9.*(l-x).^9;                               f10= @(x,l) x.^10.*(l-x).^10.*(l/2-x).^10; 

i write function:

syms x l   f=cell(10,1);  fun=cell(10,1);  i=1:10      if mod(i,2) ~= 0          f{i}=['x','.^',num2str(i),'.*','(l-x)','.^',num2str(i)];      else          f{i}=['x','.^',num2str(i),'.*','(l-x)','.^',num2str(i),'.*','(l/2-x)','.^',num2str(i)];      end  end   i=1:10      j=1:10          if mod(i,2) ~= 0 & mod(j,2) ~= 0              fs = ['(x)','x','.^',num2str(i),'.*','(l-x)','.^',num2str(i),'*','x','.^',num2str(j),'.*','(l-x)','.^',num2str(j)];              fs = str2func (fs)             fjnew = str2func(['(x)','x','.^',num2str(j),'.*','(l-x)','.^',num2str(j)])              fj_diff = diff(fjnew,x) 

when run error :

undefined function '(x)x.^1.*(l-x).^1' input arguments of type 'sym'.  error in sym>funchandle2ref (line 1172)     s = x(s{:});  error in sym>tomupad (line 989)     x = funchandle2ref(x);  error in sym (line 142)             s.s = tomupad(x,'');  error in sym/privresolveargs (line 810)                     argout{k} = sym(arg);  error in sym/diff (line 21) args = privresolveargs(s,varargin{:}); 

your sincerely saeed nasiri

i found problems.

  1. the character "l" not character "1". 1 of them l , other number. wrote "l" \ell\ instead of "1" \one\ in (l-x). edit realised want l not 1.

  2. function handles not (x)x.^2 (random example), @(x)x.^2! forgot add "@" in calls, =['(x)' should =['@(x)'

3.your functions (as described in post), variable respect x , l. thus, need function handle that. change =['@(x)' =['@(x,l)'.

  1. iam not sure if aware of this, dont use practically of parts of code. code can reduced 4 lines , same. fs,fs adn f never use.:

the result of code same as:

for i=1:10     j=1:10         if mod(i,2) ~= 0 & mod(j,2) ~= 0             fjnew = str2func(['@(x,l)','x','.^',num2str(j),'.*','(l-x)','.^',num2str(j)])             fj_diff = diff(fjnew,x)         end     end end 

so unless doing more stuff later, there no need compute stuff unnecessarily.


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -