Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用Matlab绘制条件下的函数_Matlab_Matlab Figure - Fatal编程技术网

用Matlab绘制条件下的函数

用Matlab绘制条件下的函数,matlab,matlab-figure,Matlab,Matlab Figure,我正在寻找解决此问题的方法: 考虑一个函数f(x)=2x+1,其中x属于[01000]。绘制f的代表性曲线作为x的函数,这样,如果| | f(x)| |下面的代码就可以实现这一点: % Obtain an array with the desired values y = myfunc(x); % Get a list of indices to refer to values of y % meeting your criteria (there are alternative ways

我正在寻找解决此问题的方法:


考虑一个函数f(x)=2x+1,其中x属于[01000]。绘制f的代表性曲线作为x的函数,这样,如果| | f(x)| |下面的代码就可以实现这一点:

% Obtain an array with the desired values
y = myfunc(x);

% Get a list of indices to refer to values of y 
% meeting your criteria (there are alternative ways
% to do it
indInAbs = find((abs(y)<3));
indOutAbs = find((abs(y)>=3));

% Create two arrays with y-values
% within the desired range
yInAbs = y(indInAbs);
xInAbs = x(indInAbs);

% Create two arrays with y-values
% outside the desired range
yOutAbs = y(indOutAbs);
xOutAbs = x(indOutAbs);

% Plot the values
figure(1);
hold on;
plot( xInAbs, yInAbs, 'r')
plot( xOutAbs, yOutAbs, 'b')
legend('in abs', 'out abs', 'location', 'best')
%获取具有所需值的数组
y=myfunc(x);
%获取索引列表以引用y的值
%满足您的标准(有其他方法
%去做
indInAbs=find((abs(y)=3));
%创建两个具有y值的数组
%在期望范围内
yInAbs=y(indInAbs);
xInAbs=x(indInAbs);
%创建两个具有y值的数组
%超出所需范围
yOutAbs=y(indOutAbs);
xOutAbs=x(indOutAbs);
%绘制值
图(1);
等等
绘图(xInAbs,yInAbs,'r')
绘图(xOutAbs,yOutAbs,'b')
图例('in-abs'、'out-abs'、'location'、'best')

还有其他更高效、更优雅的方法。但是,这是一个快速而肮脏的解决方案。

您的阈值不能太低,否则它没有足够的数据来绘制(如果阈值=3)或看不到蓝色部分。这里我使用500,以便您可以看到

function plotSeparate
    clc
    close all
    k=0
    threshold=500
    for x=1:0.5:1000
        k=k+1
        y=f(x)
        if abs(y)<threshold
            t(k,:)=[x,y];
        else
            s(k,:)=[x,y];
        end
    end
    plot(s(:,1),s(:,2),'-r',t(:,1),t(:,2),'-b')
end

function y=f(x)
    y=2*x+1;
end
函数
clc
全部关闭
k=0
阈值=500
对于x=1:0.5:1000
k=k+1
y=f(x)

如果abs(y)你能粘贴到目前为止你尝试过的代码吗?对于
x
,介于
0
1000
之间,对于给定的线性函数…你应该看到的唯一红色部分是与
x<1
相对应的部分。A)不要轻易回答问题,因为这违反了堆栈溢出模型。B) 不要只给出代码的答案,而是解释代码为什么做OP希望它做的事情,这意味着你应该在这篇文章中添加文本和/或注释,解释它为什么有效,特别是当你回答像这样糟糕的问题时,OP明确表示他们还不了解这项技术。至于代码本身:文本太多。只需找到一个索引并对另一个索引使用+(或-)1,而不是使用
find
两次并手动复制许多行。@Adriaan感谢您的指导,您是对的。关于你对索引+-1的评论,我认为我们搜索了一系列索引。你能澄清一下吗?我们不是在搜索范围,而是在搜索点。任何到那个点的东西都是蓝色的,超过那个点的东西都是红色的。不需要两次找到该索引。如果您发现您的
indInAbs
indOutAbs
符合
=
indInAbs+1
的定义。因此,两次使用
find
调用计算量大,但没有额外的好处,而手动复制同一点(使用1个不同的idx)最多也会造成混淆,当点改变时会很麻烦,因为你需要改变很多代码而不是一个变量来定义改变点。所以你的蓝色范围是
1:indInAbs
,红色范围是
indInAbs+1:end
。使用一个变量完成并清理,因此当您更改更改点时,需要更少的代码、更少的计算时间和更少的麻烦。@Adrian我完全同意这个示例。但是,我有两个问题:1)如果函数不是单调的和/或2)如果你的x数组没有排序怎么办?我可以从这个问题看出,我们对这个
f(x)
x=0:1000的范围并不特别感兴趣。