如何在Matlab中进行多变量函数的基本绘图?

时间:2012-10-12 22:37:29

标签: matlab

我必须计算函数的极限

(c^(2n) - 1)/(c^(2n) + 1) as n = 1,2,3 ...进入无限

函数的行为将取决于参数c,我想说明的是通过绘制c的不同值的序列的前100个(或左右)值 - 比如三个图,一个用于{{1 }},一个用于c = 1,一个用于-1 < c < 1,如果可能的话,只需一张“图片”。

在MATLAB中处理此问题的最佳方法是什么?

2 个答案:

答案 0 :(得分:3)

您需要使用内联函数,保存所有和图例。这是一个简单的例子

n=1:100;
f = @(c) (c.^(2.*n) - 1)./(c.^(2.*n) + 1); 
hold all;
plot(n,f(0.7),'.-');
plot(n,f(0.9),'.-');
plot(n,f(0.95),'.-');
plot(n,f(1),'.-');
plot(n,f(1.05),'.-');
plot(n,f(1.3),'.-');
plot(n,f(1.1),'.-');
legend('c=0.7','c=0.9','c=0.95','c=1.0','c=1.05','c=1.3','c=1.1');

这个输出看起来像这样 enter image description here

答案 1 :(得分:2)

我会做类似的事情:

% Clean up, this isn't necessary if you're throwing this in a function
%   and not a script.
close all
clear all
clc

% Define your function
f = @(c,n)( (c^(2*n) - 1)/(c^(2*n) + 1) );

% An array containing the values of c you want to use
C = [0.5, 1, 1.5, 2];

% N will contain 500 points, equally spaced between 1 and 20.
%   (Modify these arguments as necessary)
N = linspace(1,20,500);

% Initialize an output matrix
Y = zeros(length(N), length(C));
for i = [1:length(C)]
    c = C(i);

    for j = [1:length(N)]
        n = N(j);

        % Compute value of function
        y = f(c,n);

        % Store it
        Y(j,i) = y;
    end%for
end%for

% Plot it
plot(N,Y);

% Generate Legend
lt = cell(1,length(C));
for i = [1:length(C)]
    lt{i} = sprintf('C = %s', num2str(C(i)));
end%for
legend(lt);

您可以通过修改函数f来接受和返回向量来优化它,但是这个显式版本可能会更好地显示正在发生的事情。