如何在MATLAB中对图的因变量执行语句?

问题描述:

我想知道如何从间隔中抓取一个特定的数字来测试它,然后可以在一个图形下构建不同的功能。例如(在这种情况下,变量“x”),如何在MATLAB中对图的因变量执行语句?

x 0:.5:5; 

Ids=ones(x); 
figure;hold on; 

for n = 1:5 
    if(x < 3.0) %problem here 
     Ids(n) = plot(x,x.^x); 
    else 
     if (x > 4.0) %and here 
      Ids(n) = plot(x,-x.^x); 
     end 
    end 
end 

编辑

我真的想在MATLAB做的是能够做到以下分段函数:

y(x) = { 0     (t - 5) < 0 
     { (t - 5)*(t - x)  x < (t - 5) 
     { (t + x^2)   x >= (t - 5) 

自从x = 0:.5:10t = 0:.1:10以来,我似乎不知道如何绘制此功能。我知道如何在没有t的情况下做到这一点,但当包含t并且与x相比有不同的间隔时,我会迷路。

+0

Y_Y,你能澄清你想做什么?正如gnovice写道的,从你的代码中不清楚你想要做什么。你能用'文字'来写你想做什么吗? gnovice的答案中给出的函数f(x)是你要找的吗? – 2010-12-06 04:52:04

这是从你的代码有点不清楚你正在尝试做的,但现在看来,要建立并绘制函数f(x)具有以下形式:

f(x) = [ x  for 3 <= x <= 4 
     [ x^x for x < 3 
     [ -x^x for x > 4 

如果这是你想要的这样做,你可以做以下使用logical indexing

x = 0:0.5:5; %# 11 points spaced from 0 to 5 in steps of 0.5 
y = x;  %# Initialize y 
index = x < 3;     %# Get a logical index of points less than 3 
y(index) = x(index).^x(index); %# Change the indexed points 
index = x > 4;     %# Get a logical index of points greater then 4 
y(index) = -x(index).^x(index); %# Change the indexed points 
plot(x,y);      %# Plot y versus x 

你可能会寻找分段多项式:否则http://www.mathworks.com/help/techdoc/ref/mkpp.html

,我会建议做两个向量,“X”和“Y”,可以这么说,并通过X迭代和应用的条件下灌装y和结果,然后将y绘制在x上。这将避免需要保持阴谋。

如果要为图形制作动画,请将plot()添加到for循环,然后加上“drawnow”。这已经有一段时间了,因为我不得不对动画进行动画处理,所以我会建议用于汲取和动画的教程。

+0

如何使用分段函数'mkpp'来做到这一点? – 2010-12-05 09:26:43