数学建模社区-数学中国

标题: 函数绘图---例题 [打印本页]

作者: 森之张卫东    时间: 2015-9-18 21:13
标题: 函数绘图---例题
创建一个函数的函数,它能够画出所有只有一个自变量的MATLAB函数的图象,自变量的范围是用户指定的始值和终值。
答案:
这个函数有两个输入参数,第一个是要画的函数的函数名,第二个是两元素向量,它指明了画图的取值范围。
1.陈述问题
创建一个函数的函数,它能够画出所有只有一个自变量的MATLAB函数的图象,自变量的范围由用户指定。
2.定义输入输出函数的输入有两个
(1)包含有函数名的字符串
(2)包含有起始值和终值的2元素向量函数的输出是所要画的图象
3.设计算法这个函数可以分为4大步:
Check for a legal number of arguments
Check that the second argument has two elements
Calculate the value of the function between the start and stoppoints
Plot and label the function
第三四大步的伪代码如下
n_steps ← 100
step_size ← (xlim(2) – xlim(1)) / nsteps
x ← xlim(1):step_size:xlim(2)
y ← feval(fun, x)
plot(x, y)
title(['bf Plot of function ' fun ' (x)'])
xlabel('\bfx;)
ylabel(['bf ' fun ' (x)'])




作者: 森之张卫东    时间: 2015-9-18 21:13
  1. function quickplot(fun,xlim)
  2. %QUICKPLOT Generate quick plot of a function
  3. % Function QUICKPLOT generates a quick plot
  4. % of a function contained in a external mfile,
  5. % between user-specified x limits.
  6. % Define variables:
  7. % fun                   --Function to plot
  8. % msg                   --Error message
  9. % n_steps               --Number of steps to plot
  10. % step_size             --Step size
  11. % x                     --X-values to plot
  12. % y                     --Y-values to plot
  13. % xlim                  --Plot x limits
  14. % Record of revisions:
  15. % Date Programmer Description of change
  16. % ==== ========== =====================
  17. % 12/17/98 S. J. Chapman Original code
  18. % Check for a legal number of input arguments.
  19. msg = nargchk(2,2,nargin);
  20. error(msg);
  21. % Check the second argument to see if it has two
  22. % elements. Note that this double test allows the
  23. % argument to be either a row or a column vector.
  24. if ( size(xlim,1) == 1 & size(xlim,2) == 2 ) | ...
  25. ( size(xlim,1) == 2 & size(xlim,2) == 1 )
  26.     % Ok        --continue processing.
  27.     n_steps = 100;
  28.     step_size = (xlim(2) - xlim(1)) / n_steps;
  29.     x = xlim(1):step_size:xlim(2);
  30.     y = feval(fun,x);
  31.     plot(x,y);
  32.     title(['\bfPlot of function ' fun '(x)']);
  33.     xlabel('\bfx');
  34.     ylabel(['\bf' fun '(x)']);
  35. else
  36.     % Else wrong number of elements in xlim.
  37.     error('Incorrect number of elements in xlim.');
  38. end
复制代码


作者: 森之张卫东    时间: 2015-9-18 21:14
附件:详细内容。

画图像的函数.doc

63 KB, 下载次数: 0, 下载积分: 体力 -2 点

详细内容






欢迎光临 数学建模社区-数学中国 (http://www.madio.net/) Powered by Discuz! X2.5