森之张卫东 发表于 2015-9-18 21:13

函数绘图---例题

创建一个函数的函数,它能够画出所有只有一个自变量的MATLAB函数的图象,自变量的范围是用户指定的始值和终值。答案:这个函数有两个输入参数,第一个是要画的函数的函数名,第二个是两元素向量,它指明了画图的取值范围。1.陈述问题创建一个函数的函数,它能够画出所有只有一个自变量的MATLAB函数的图象,自变量的范围由用户指定。2.定义输入输出函数的输入有两个(1)包含有函数名的字符串(2)包含有起始值和终值的2元素向量函数的输出是所要画的图象3.设计算法这个函数可以分为4大步:Check for a legal number of argumentsCheck that the second argument has two elementsCalculate the value of the function between the start and stoppointsPlot and label the function第三四大步的伪代码如下n_steps ← 100step_size ← (xlim(2) – xlim(1)) / nstepsx ← 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

function quickplot(fun,xlim)
%QUICKPLOT Generate quick plot of a function
% Function QUICKPLOT generates a quick plot
% of a function contained in a external mfile,
% between user-specified x limits.
% Define variables:
% fun                   --Function to plot
% msg                   --Error message
% n_steps               --Number of steps to plot
% step_size             --Step size
% x                     --X-values to plot
% y                     --Y-values to plot
% xlim                  --Plot x limits
% Record of revisions:
% Date Programmer Description of change
% ==== ========== =====================
% 12/17/98 S. J. Chapman Original code
% Check for a legal number of input arguments.
msg = nargchk(2,2,nargin);
error(msg);
% Check the second argument to see if it has two
% elements. Note that this double test allows the
% argument to be either a row or a column vector.
if ( size(xlim,1) == 1 & size(xlim,2) == 2 ) | ...
( size(xlim,1) == 2 & size(xlim,2) == 1 )
    % Ok        --continue processing.
    n_steps = 100;
    step_size = (xlim(2) - xlim(1)) / n_steps;
    x = xlim(1):step_size:xlim(2);
    y = feval(fun,x);
    plot(x,y);
    title(['\bfPlot of function ' fun '(x)']);
    xlabel('\bfx');
    ylabel(['\bf' fun '(x)']);
else
    % Else wrong number of elements in xlim.
    error('Incorrect number of elements in xlim.');
end

森之张卫东 发表于 2015-9-18 21:14

附件:详细内容。
页: [1]
查看完整版本: 函数绘图---例题