设为首页收藏本站

EPS数据狗论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1162|回复: 0

图形界面函数计算器

[复制链接]

2

主题

71

金钱

90

积分

新手用户

发表于 2018-9-17 09:13:29 | 显示全部楼层 |阅读模式
  1. function funtool(keyword,varargin)
  2. %图形界面函数计算器,能进行函数各类初等运算、积分、微分、作图。
  3. %FUNTOOL A function calculator.
  4. %   FUNTOOL is an interactive graphing calculator that manipulates
  5. %   functions of a single variable.  At any time, there are two functions
  6. %   displayed, f(x) and g(x).  The result of most operations replaces f(x).
  7. %
  8. %   The controls labeled 'f = ' and 'g = ' are editable text that may
  9. %   be changed at any time to install a new function.  The control
  10. %   labeled 'x = ' may be changed to specify a new domain.  The control
  11. %   labeled 'a = ' may be changed to specify a new value of a parameter.
  12. %
  13. %   The top row of control buttons are unary function operators that
  14. %   involve only f(x).  These operators are:
  15. %      df/dx     - Symbolically differentiate f(x).
  16. %      int f     - Symbolically integrate f(x).
  17. %      simple f  - Simplify the symbolic expression, if possible.
  18. %      num f     - Extract the numerator of a rational expression.
  19. %      den f     - Extract the denominator of a rational expression.
  20. %      1/f       - Replace f(x) by 1/f(x).
  21. %      finv      - Replace f(x) by its inverse function.
  22. %
  23. %   The operators int(f) and finv may fail if the corresponding symbolic
  24. %   expressions do not exist in closed form.
  25. %
  26. %   The second row of buttons translate and scale f(x) by the parameter 'a'.
  27. %   The operations are:
  28. %      f + a    - Replace f(x) by f(x) + a.
  29. %      f - a    - Replace f(x) by f(x) - a.
  30. %      f * a    - Replace f(x) by f(x) * a.
  31. %      f / a    - Replace f(x) by f(x) / a.
  32. %      f ^ a    - Replace f(x) by f(x) ^ a.
  33. %      f(x+a)   - Replace f(x) by f(x + a).
  34. %      f(x*a)   - Replace f(x) by f(x * a).
  35. %
  36. %   The third row of buttons are binary function operators that
  37. %   operate on both f(x) and g(x).  The operations are:
  38. %      f + g  - Replace f(x) by f(x) + g(x).
  39. %      f - g  - Replace f(x) by f(x) - g(x).
  40. %      f * g  - Replace f(x) by f(x) * g(x).
  41. %      f / g  - Replace f(x) by f(x) / g(x).
  42. %      f(g)   - Replace f(x) by f(g(x)).
  43. %      g = f  - Replace g(x) by f(x).
  44. %      swap   - Interchange f(x) and g(x).
  45. %
  46. %   The first three buttons in the fourth row manage a list of functions.
  47. %   The Insert button places the current active function in the list.
  48. %   The Cycle button rotates the function list.
  49. %   The Delete button removes the active function from the list.  
  50. %   The list of functions is named fxlist.  A default fxlist containing
  51. %           several interesting functions is provided.
  52. %
  53. %   The Reset button sets f, g, x, a and fxlist to their initial values.
  54. %   The Help button prints this help text.  
  55. %
  56. %   The Demo button poses the following challenge: Can you generate the
  57. %   function sin(x) without touching the keyboard, using just the mouse?
  58. %   The demo does it with a reset and then nine clicks.  If you can do
  59. %   it with fewer clicks, please send e-mail to moler@mathworks.com.
  60. %
  61. %   The Close button closes all three windows.
  62. %
  63. %   See also EZPLOT.

  64. %   Copyright (c) 1993-98 by The MathWorks, Inc.
  65. %       $Revision: 1.11 $  $Date: 1997/11/29 01:06:25 $

  66. %Implementation Notes:
  67. %   f,g, and a are syms.
  68. %   x is a string. fxlist is a string matrix.
  69. %
  70. %   The values of f, g, a, and x are stored in the UserData of the text
  71. %     objects "f", "g", "a", and "x", respectively. These text objects are
  72. %     tagged "fobj", "gobj", "aobj", and "xstr", respectively.
  73. %   fxlist is stored in the UserData of the control panel figure, which is
  74. %     tagged as "figp".
  75. %   The edit text boxes for f, g, x, and a are tagged "Sf", "Sg", "Sx", and
  76. %     "Sa", respectively.
  77. %   The initial values of f, g, x, a, and fxlist are stored in a structure
  78. %     called init that has fields .f, .g, .x, .a and .l. The structure is
  79. %     stored in the UserData of the Reset button.

  80. %%%%%%%%%%%%%%%%%%%%%%%%%%  Initialization section.
  81. if nargin == 0

  82. init.f = 'x';
  83. init.g = '1';
  84. init.x = '[-2*pi, 2*pi]';
  85. init.a = '1/2';
  86. init.l = str2mat( ...
  87.     '1/(5+4*cos(x));  [-2*pi, 2*pi]', ...
  88.     'cos(x^3)/(1+x^2);  [-2*pi, 2*pi]', ...
  89.     'x^4*(1-x)^4/(1+x^2);  [0, 1]', ...
  90.     'x^7-7*x^6+21*x^5-35*x^4+35*x^3-21*x^2+7*x-1;  [0.985, 1.015]', ...
  91.     'log(abs(sqrt(x)));  [0, 2]', ...
  92.     'tan(sin(x))-sin(tan(x));  [-pi, pi]');

  93. f = sym(init.f);
  94. g = sym(init.g);
  95. a = sym(init.a);
  96. x = init.x;
  97. fxlist = init.l;

  98. % Macros
  99. blanks = '  ';
  100. p = .12*(1:7) - .02;
  101. q = .60 - .14*(1:4);
  102. r = [.10 .10];

  103. % Position the two figures and the control panel.
  104. figf = figure('units','normalized','pos',[.01 .50 .48 .48],...
  105.               'menu','none','tag','figf');
  106. figg = figure('units','normalized','pos',[.50 .50 .48 .48],...
  107.               'menu','none', 'tag','figg');
  108. figp = figure('units','normalized','pos',[.25 .05 .50 .40],'menu','none', ...
  109.               'tag','figp',...
  110.               'Color',get(0,'DefaultUIControlBackgroundColor'), ...
  111.               'DefaultUIControlUnit','norm','UserData',fxlist);

  112. % Plot f(x) and g(x).
  113. figure(figf)
  114. ezplot(f,x,figf)
  115. figure(figg)
  116. ezplot(g,x,figg)

  117. % Control panel
  118. figure(figp)
  119. uicontrol('style','frame','pos',[0.01 0.60 0.98 0.38]);
  120. uicontrol('style','frame','pos',[0.01 0.01 0.98 0.58]);
  121. uicontrol('style','text','string','f = ','pos',[0.04 0.86 0.09 0.10],...
  122.     'tag','fobj','UserData',f);
  123. uicontrol('style','text','string','g = ','pos',[0.04 0.74 0.09 0.10],...
  124.     'tag','gobj','UserData',g);
  125. uicontrol('style','text','string','x = ','pos',[0.04 0.62 0.09 0.10],...
  126.     'tag','xstr','UserData',x);
  127. uicontrol('style','text','string','a = ','pos',[0.54 0.62 0.09 0.10],...
  128.     'tag','aobj','UserData',a);
  129. uicontrol('pos',[.12 .86 .82 .10],'style','edit','horiz','left', ...
  130.     'backgroundcolor','white', ...
  131.     'string', [blanks char(f)],'tag','Sf', ...
  132.     'CallBack','funtool Sfcallback');
  133. uicontrol('pos',[.12 .74 .82 .10],'style','edit','horiz','left', ...
  134.     'backgroundcolor','white', ...
  135.     'string', [blanks char(g)], 'tag','Sg',...
  136.     'CallBack','funtool Sgcallback');
  137. uicontrol('pos',[.12 .62 .32 .10],'style','edit','horiz','left', ...
  138.     'backgroundcolor','white','string',[blanks x], 'tag','Sx',...
  139.     'CallBack','funtool Sxcallback');
  140. uicontrol('pos',[.62 .62 .32 .10],'style','edit','horiz','left', ...
  141.     'backgroundcolor','white', 'tag','Sa',...
  142.     'string',[blanks char(a)],'CallBack','funtool Sacallback');

  143. % Top row of unary operators.
  144. uicontrol('pos',[p(1) q(1) r],'string','df/dx', ...
  145.    'CallBack','funtool(''row1'',''diff'')');
  146. uicontrol('pos',[p(2) q(1) r],'string','int f', ...  
  147.    'CallBack','funtool(''row1'',''int'')');
  148. uicontrol('pos',[p(3) q(1) r],'string','simple f', ...  
  149.    'CallBack','funtool(''row1'',''simple'')');
  150. uicontrol('pos',[p(4) q(1) r],'string','num f', ...  
  151.    'CallBack','funtool(''row1'',''num'')');
  152. uicontrol('pos',[p(5) q(1) r],'string','den f', ...  
  153.    'CallBack','funtool(''row1'',''den'')');
  154. uicontrol('pos',[p(6) q(1) r],'string','1/f', ...  
  155.    'CallBack','funtool(''row1'',''1/f'')');
  156. uicontrol('pos',[p(7) q(1) r],'string','finv', ...  
  157.    'CallBack','funtool(''row1'',''finverse'')');

  158. % Second row of unary operators.  
  159. uicontrol('pos',[p(1) q(2) r],'string','f+a', ...
  160.    'CallBack','funtool(''row2'',''f+a'')');
  161. uicontrol('pos',[p(2) q(2) r],'string','f-a', ...
  162.    'CallBack','funtool(''row2'',''f-a'')');
  163. uicontrol('pos',[p(3) q(2) r],'string','f*a', ...
  164.    'CallBack','funtool(''row2'',''f*a'')');
  165. uicontrol('pos',[p(4) q(2) r],'string','f/a', ...
  166.    'CallBack','funtool(''row2'',''f/a'')');
  167. uicontrol('pos',[p(5) q(2) r],'string','f^a', ...
  168.    'CallBack','funtool(''row2'',''f^a'')');
  169. uicontrol('pos',[p(6) q(2) r],'string','f(x+a)', ...
  170.    'CallBack','funtool(''row2'',''f(x+a)'')');
  171. uicontrol('pos',[p(7) q(2) r],'string','f(x*a)', ...
  172.    'CallBack','funtool(''row2'',''f(x*a)'')');

  173. % Third row, binary operators.
  174. uicontrol('pos',[p(1) q(3) r],'string','f + g', ...
  175.     'CallBack','funtool(''row3'',''f+g'')');
  176. uicontrol('pos',[p(2) q(3) r],'string','f - g', ...
  177.     'CallBack','funtool(''row3'',''f-g'')');
  178. uicontrol('pos',[p(3) q(3) r],'string','f * g', ...
  179.     'CallBack','funtool(''row3'',''f*g'')');
  180. uicontrol('pos',[p(4) q(3) r],'string','f / g', ...
  181.     'CallBack','funtool(''row3'',''f/g'')');
  182. uicontrol('pos',[p(5) q(3) r],'string','f(g)', ...
  183.     'CallBack','funtool(''row3'',''f(g)'')');
  184. uicontrol('pos',[p(6) q(3) r],'string','g = f', ...
  185.     'CallBack','funtool(''row3'',''g=f'')');
  186. uicontrol('pos',[p(7) q(3) r],'string','swap', ...
  187.     'CallBack','funtool(''row3'',''swap'')');

  188. % Fourth row, auxilliary controls.
  189. uicontrol('pos',[p(1) q(4) r],'string','Insert','CallBack','funtool Insert');
  190. uicontrol('pos',[p(2) q(4) r],'string','Cycle','CallBack','funtool Cycle');
  191. uicontrol('pos',[p(3) q(4) r],'string','Delete','CallBack','funtool Delete');
  192. uicontrol('pos',[p(4) q(4) r],'string','Reset','Tag','reset', ...
  193.     'UserData', init, 'CallBack', 'funtool Reset');
  194. uicontrol('pos',[p(5) q(4) r],'string','Help', ...
  195.     'CallBack','helpwin funtool');
  196. uicontrol('pos',[p(6) q(4) r],'string','Demo', ...
  197.     'CallBack','funtool Demo');
  198. uicontrol('pos',[p(7) q(4) r],'string','Close', ...
  199.     'CallBack','funtool close');

  200. %%%%%%%%%%%%%%%%%%%%%%%%%%  end of Initialization section

  201. else
  202. switch keyword

  203. %%%%%%%%%%%%%%%%%%%%%%%%%%  Callback for top row of unary operators.
  204. case 'row1'
  205.    figp = findobj(0,'tag','figp');
  206.    fhndl = findobj(figp,'tag','fobj');
  207.    f = get(fhndl,'UserData');
  208.    x = get(findobj(figp,'tag','xstr'),'UserData');
  209.    figf = findobj(0,'tag','figf');

  210.    switch varargin{1}
  211.      case 'diff'
  212.        f = diff(f);
  213.      case 'int'
  214.        f = int(f);
  215.      case 'simple'
  216.        f = simple(f);
  217.      case 'num'
  218.        [f,ans] = numden(f);
  219.      case 'den'
  220.        [ans,f] = numden(f);
  221.      case '1/f'
  222.        f = 1/f;
  223.      case 'finverse'
  224.        f = finverse(f);
  225.    end

  226.    set(fhndl,'UserData',f);
  227.    ezplot(f,x,figf)
  228.    blanks = '  ';
  229.    set(findobj(figp,'tag','Sf'),'string',[blanks char(f)]);
  230.    
  231. %%%%%%%%%%%%%%%%%%%%%%%%%%  Callback for second row of unary operators.
  232. case 'row2'
  233.    figp = findobj(0,'tag','figp');
  234.    fhndl = findobj(figp,'tag','fobj');
  235.    f = get(fhndl,'UserData');
  236.    x = get(findobj(figp,'tag','xstr'),'UserData');
  237.    a = get(findobj(figp,'tag','aobj'),'UserData');
  238.    figf = findobj(0,'tag','figf');

  239.    switch varargin{1}
  240.      case 'f+a'
  241.        f = f+a;
  242.      case 'f-a'
  243.        f = f-a;
  244.      case 'f*a'
  245.        f = f*a;
  246.      case 'f/a'
  247.        f = f/a;
  248.      case 'f^a'
  249.        f = f^a;
  250.      case 'f(x+a)'
  251.        f = subs(f,sym('x'),sym('x')+a);
  252.      case 'f(x*a)', ...
  253.        f = subs(f,sym('x'),sym('x')*a);
  254.    end

  255.    set(fhndl,'UserData',f);
  256.    ezplot(f,x,figf)
  257.    blanks = '  ';
  258.    set(findobj(figp,'tag','Sf'),'string',[blanks char(f)]);
  259.    
  260. %%%%%%%%%%%%%%%%%%%%%%%%%%  Callback for third row, binary operators.
  261. case 'row3'
  262.    blanks = '  ';

  263.    % Get variables.
  264.    figp = findobj(0,'tag','figp');
  265.    fhndl = findobj(figp,'tag','fobj');
  266.    ghndl = findobj(figp,'tag','gobj');
  267.    f = get(fhndl,'UserData');
  268.    g = get(ghndl,'UserData');
  269.    x = get(findobj(figp,'tag','xstr'),'UserData');
  270.    a = get(findobj(figp,'tag','aobj'),'UserData');
  271.    figf = findobj(0,'tag','figf');
  272.    figg = findobj(0,'tag','figg');

  273.    if strcmp(varargin{1}, 'g=f')
  274.      g = f;
  275.      set(ghndl,'UserData',g);
  276.      ezplot(g,x,figg)
  277.      set(findobj(figp,'tag','Sg'),'string',[blanks char(g)]);

  278.    elseif strcmp(varargin{1}, 'swap')
  279.      h = f; f = g; g = h;
  280.      set(fhndl,'UserData',f);
  281.      ezplot(f,x,figf)
  282.      set(findobj(figp,'tag','Sf'),'string',[blanks char(f)]);
  283.      set(ghndl,'UserData',g);
  284.      ezplot(g,x,figg)
  285.      set(findobj(figp,'tag','Sg'),'string',[blanks char(g)]);

  286.    else
  287.      switch varargin{1}
  288.        case 'f+g'
  289.          f = f+g;
  290.        case 'f-g'
  291.          f = f-g;
  292.        case 'f*g'
  293.          f = f*g;
  294.        case 'f/g'
  295.          f = f/g;
  296.        case 'f(g)'
  297.          f = compose(f,g);
  298.      end

  299.      set(fhndl,'UserData',f);
  300.      ezplot(f,x,figf)
  301.      set(findobj(figp,'tag','Sf'),'string',[blanks char(f)]);
  302.    end

  303. %%%%%%%%%%%%%%%%%%%%%%%%%% Callback for F's edit text box.
  304. case 'Sfcallback'
  305.    figp = findobj(0,'tag','figp');
  306.    f = sym(get(gco,'string'));
  307.    fhndl = findobj(figp,'tag','fobj');
  308.    set(fhndl,'UserData',f);
  309.    x = get(findobj(figp,'tag','xstr'),'UserData');
  310.    figf = findobj(0,'tag','figf');
  311.    ezplot(f,x,figf)

  312. %%%%%%%%%%%%%%%%%%%%%%%%%% Callback for G's edit text box.
  313. case 'Sgcallback'
  314.    figp = findobj(0,'tag','figp');
  315.    g = sym(get(gco,'string'));
  316.    ghndl = findobj(figp,'tag','gobj');
  317.    set(ghndl,'UserData',g);
  318.    x = get(findobj(figp,'tag','xstr'),'UserData');
  319.    figg = findobj(0,'tag','figg');
  320.    ezplot(g,x,figg)

  321. %%%%%%%%%%%%%%%%%%%%%%%%%% Callback for A's edit text box.
  322. case 'Sacallback'
  323.    a = sym(get(gco,'String'));
  324.    set(findobj(gcf,'tag','aobj'),'UserData',a);

  325. %%%%%%%%%%%%%%%%%%%%%%%%%% Callback for X's edit text box.
  326. case 'Sxcallback'

  327.    figp = findobj(0,'tag','figp');
  328.    x = get(gco,'String');

  329.    % add brackets if needed
  330.    if isempty(x=='['),
  331.       x = ['[' x ']'];
  332.       blanks = '  ';
  333.       set(gco,'string',[blanks x]),
  334.    end;
  335.    set(findobj(figp,'tag','xstr'),'UserData',x);

  336.    fhndl = findobj(figp,'tag','fobj');
  337.    ghndl = findobj(figp,'tag','gobj');
  338.    f = get(fhndl,'UserData');
  339.    g = get(ghndl,'UserData');
  340.    a = get(findobj(figp,'tag','aobj'),'UserData');
  341.    figf = findobj(0,'tag','figf');
  342.    figg = findobj(0,'tag','figg');
  343.    
  344.    ezplot(f,x,figf)
  345.    ezplot(g,x,figg)

  346. %%%%%%%%%%%%%%%%%%%%%%%%%% Callback for Insert button.
  347. case 'Insert'
  348.    f = get(findobj(gcf,'tag','fobj'),'UserData');
  349.    x = get(findobj(gcf,'tag','xstr'),'UserData');
  350.    fxlist = get(gcf,'UserData');
  351.    fxlist = str2mat(fxlist,[char(f) ';  ' x]);
  352.    set(gcf,'UserData',fxlist);

  353. %%%%%%%%%%%%%%%%%%%%%%%%%% Callback for Cycle button.
  354. case 'Cycle'

  355.    % Get variables.
  356.    figp = gcf;
  357.    figf = findobj(0,'tag','figf');
  358.    fxlist = get(figp,'UserData');

  359.    fx = fxlist(1,:);
  360.    fx(find(fx==' ')) = [];
  361.    k = find(fx == ';');
  362.    fstr = fx(1:k-1); f = sym(fstr);
  363.    set(findobj(figp,'tag','fobj'),'UserData',f);
  364.    x = fx(k+1:length(fx));
  365.    set(findobj(figp,'tag','xstr'),'UserData',x);

  366.    blanks = '  ';
  367.    set(findobj(figp,'tag','Sx'),'string',[blanks x]);
  368.    set(findobj(figp,'tag','Sf'),'string',[blanks fstr]);
  369.    ezplot(f,x,figf);
  370.    k = [2:size(fxlist,1),1];
  371.    fxlist = fxlist(k,:);
  372.    set(figp,'UserData',fxlist);

  373. %%%%%%%%%%%%%%%%%%%%%%%%%% Callback for Delete button.
  374. case 'Delete'

  375.    % Get variables.
  376.    figp = gcf;
  377.    f = get(findobj(figp,'tag','fobj'),'UserData');
  378.    x = get(findobj(figp,'tag','xstr'),'UserData');
  379.    fxlist = get(figp,'UserData');

  380.    fx = [char(f) ';' x];
  381.    fx(find(fx==' ')) = [];
  382.    for k = 1:size(fxlist,1),
  383.       element = fxlist(k,:);
  384.       element(find(element==' ')) = [];
  385.       if strcmp(fx,element)
  386.         fxlist(k,:) = [];
  387.         break
  388.       end
  389.    end;
  390.    if isempty(fxlist), fxlist = '0-0;  [0,1]'; end

  391.    set(figp,'UserData',fxlist);

  392. %%%%%%%%%%%%%%%%%%%%%%%%%% Callback for Reset button.
  393. case 'Reset'
  394.    blanks = '  ';

  395.    % Get variables.
  396.    figp = gcf;
  397.    figf = findobj(0,'tag','figf');
  398.    figg = findobj(0,'tag','figg');
  399.    init = get(findobj(figp,'tag','reset'),'UserData');

  400.    set(findobj(figp,'tag','fobj'),'UserData',sym(init.f));
  401.    set(findobj(figp,'tag','Sf'),'String',[blanks init.f]);
  402.    ezplot(sym(init.f),init.x,figf);

  403.    set(findobj(figp,'tag','gobj'),'UserData',sym(init.g));
  404.    set(findobj(figp,'tag','Sg'),'String',[blanks init.g]);
  405.    ezplot(sym(init.g),init.x,figg);

  406.    set(findobj(figp,'tag','xstr'),'UserData',init.x);
  407.    set(findobj(figp,'tag','Sx'),'string',[blanks init.x]);

  408.    set(findobj(figp,'tag','aobj'),'UserData',sym(init.a));
  409.    set(findobj(figp,'tag','Sa'),'string',[blanks init.a]);

  410.    set(figp,'UserData',init.l);

  411.    % Reset all buttons to default bkgd color.
  412.    set(findobj(figp,'Style','pushbutton'),'BackgroundColor','default');

  413. %%%%%%%%%%%%%%%%%%%%%%%%%% Callback for Close button.
  414. case 'close'
  415.    close(findobj(0,'tag','figf'));
  416.    close(findobj(0,'tag','figg'));
  417.    close(findobj(0,'tag','figp'));

  418. %%%%%%%%%%%%%%%%%%%%%%%%%% Callback for Demo button.
  419. case 'Demo'

  420.    % "B" is the vector of button handles in the control panel.
  421.    % "prog" is a "program" consisting of button codes.

  422.    prog = {'Reset','f/a','int f','f + g','1/f', ...
  423.            'int f','finv','int f','df/dx','num f'};
  424.    B = findobj(gcf,'Style','pushbutton');
  425.    for k = 1: length(prog)
  426.       currB = findobj(B,'String',prog{k});
  427.       set(currB,'BackgroundColor','white');
  428.       eval(get(currB,'Callback'));
  429.       pause(1)
  430.       set(currB,'BackgroundColor','default');
  431.    end

  432. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  433. end % switch statement for callbacks
  434. end     % end of FUNTOOL
复制代码


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

站长推荐上一条 /1 下一条

客服中心
关闭
在线时间:
周一~周五
8:30-17:30
QQ群:
653541906
联系电话:
010-85786021-8017
在线咨询
客服中心

意见反馈|网站地图|手机版|小黑屋|EPS数据狗论坛 ( 京ICP备09019565号-3 )   

Powered by BFIT! X3.4

© 2008-2028 BFIT Inc.

快速回复 返回顶部 返回列表