如何在MATLAB中对图形的因变量执行语句? [英] How do I perform statements on the dependent variable of a graph in MATLAB?
问题描述
我想知道如何从间隔中获取特定数字以对其进行测试,然后能够在一个图形下构建不同的函数.例如(在本例中为'x'变量)
I would like to know how to grab a specific number from an interval to test it and later be able to built a different functions under one graph. For example (in this case the 'x' variable),
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中做的是能够执行以下分段功能:
What I really want to do in MATLAB is to be able to do the following piecewise function:
y(x) = { 0 (t - 5) < 0
{ (t - 5)*(t - x) x < (t - 5)
{ (t + x^2) x >= (t - 5)
由于x = 0:.5:10
和t = 0:.1:10
,我似乎不了解如何绘制此函数的图形.我知道如何在没有t
的情况下执行此操作,但是当包含t
并且与x
的间隔不同时,我迷路了.
I don't seem to understand how to graph this function since x = 0:.5:10
and t = 0:.1:10
. I know how to do this without the t
, but I get lost when the t
is included and has different intervals compared to the x
.
推荐答案
从您的代码中尚不清楚您要做什么,但是似乎您要创建并绘制具有以下内容的函数f(x)
形式:
It's a little unclear from your code what you are trying to do, but it appears that you want to create and plot a function f(x)
that has the following form:
f(x) = [ x for 3 <= x <= 4
[ x^x for x < 3
[ -x^x for x > 4
如果这是您要执行的操作,则可以使用逻辑索引:
If this is what you want to do, you can do the following using 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
这篇关于如何在MATLAB中对图形的因变量执行语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!