Advertisement
kimo12

Untitled

May 17th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.17 KB | None | 0 0
  1. function [func] = help(equation)
  2.     y = char(equation);
  3.     pattern = 'x';
  4.     arr= strfind(y,pattern);
  5.     n = 1;
  6.     coffecinet = 1;
  7.     while(n <= size(arr,2))
  8.         position = arr(n);
  9.         if(y(position+1) ~= '^' && y(position+1) ~= ')')
  10.                 if(y(position - 1) == '*')
  11.                     b = 2 ;
  12.                     while(y(position-b)~='+'||y(position-b)~='-')
  13.                     b=b+1;
  14.                         if(position-b==0)
  15.                             b=b-1;
  16.                             break;
  17.                         end
  18.                     end
  19.                     coff = {''};
  20.                     t=b-1;
  21.                     while(t>=2)
  22.                         coff = strcat(coff,y(position - t ));
  23.                         disp(coff);
  24.                         t=t-1;
  25.                     end
  26.                     coffecinet = str2double(coff);
  27.                     disp(coffecinet);
  28.                     if(y(position-b)=='+')
  29.                         coffecinet = coffecinet * -1 ;
  30.                     end
  31.                 else
  32.                     coffecinet = 1;
  33.                     if(y(position-1)=='+')
  34.                         coffecinet = coffecinet * -1 ;
  35.                     end
  36.                 end
  37.         end
  38.         n=n+1;
  39.     end
  40.     z = sprintf('%s%s','@(x) ',y);
  41.     func = str2func(z);
  42.     cof = num2str(coffecinet);
  43.     fun = strcat(cof,'*x');
  44.     h = sprintf('%s%s','@(x) ',fun);
  45.     func2 = str2func(h);
  46.     g1=sym(func);
  47.     g2=sym(func2);
  48.     g3 = (g1 + g2)/coffecinet;
  49.     disp(g3);
  50.     gh = char(g3);
  51.     cat = strcat('@(x) ',gh);
  52.     func=str2func(cat);
  53.     disp(func);
  54.     disp(coffecinet);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement