Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/13.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_Plot_Matlab Figure - Fatal编程技术网

Matlab:提取我绘制但尚未存储的值

Matlab:提取我绘制但尚未存储的值,matlab,plot,matlab-figure,Matlab,Plot,Matlab Figure,我有一个数学函数E,我想最小化它。我从解这16个可能的解x1,x2,…,x16中得到,其中只有两个使函数最小化(位于最小值)。使用for循环,我可以将这16个解全部插入原始函数,并通过if语句应用一些标准来选择我需要的解(如果x为实且正,如果E的一阶导数低于阈值,如果E的二阶导数为正,则绘制E vs E(x)) 这样我就只画出我感兴趣的解决方案。但是,我现在想提取我绘制的相关x。下面是一个示例MATLAB代码,它以我刚才描述的方式绘制。我想提取θ,我最终绘制的θ。怎么做 format long

我有一个数学函数E,我想最小化它。我从解这16个可能的解x1,x2,…,x16中得到,其中只有两个使函数最小化(位于最小值)。使用
for
循环,我可以将这16个解全部插入原始函数,并通过
if
语句应用一些标准来选择我需要的解(如果x为实且正,如果E的一阶导数低于阈值,如果E的二阶导数为正,则绘制E vs E(x))

这样我就只画出我感兴趣的解决方案。但是,我现在想提取我绘制的相关x。下面是一个示例MATLAB代码,它以我刚才描述的方式绘制。我想提取θ,我最终绘制的θ。怎么做

format long
theta_s = 0.77944100;
sigma = 0.50659500;
Delta = 0.52687700;

%% Defining the coefficients of the 4th degree polynomial
alpha = cos(2*theta_s);
beta = sin(2*theta_s);
gamma = 2*Delta^2/sigma^2;
a = -gamma^2 - beta^2*Delta^2 - alpha^2*Delta^2 + 2*alpha*Delta*gamma;
b = 2*alpha*gamma - 2*Delta*gamma - 2*alpha^2*Delta + 2*alpha*Delta^2 -...
    2*beta^2*Delta;
c = 2*gamma^2 - 2*alpha*Delta*gamma - 2*gamma - alpha^2 + 4*alpha*Delta +...
    beta^2*Delta^2 - beta^2 - Delta^2;
d = -2*alpha*gamma + 2*Delta*gamma + 2*alpha + 2*beta^2*Delta - 2*Delta;
e = beta^2 - gamma^2 + 2*gamma - 1;

%% Solve the polynomial numerically.
P = [a b c d e];
R = roots(P);

%% Solve r = cos(2x) for x: x = n*pi +- 1/2 * acos(r). Using n = 0 and 1.
theta   = [1/2.*acos(R) -1/2.*acos(R) pi+1/2.*acos(R) pi-1/2.*acos(R)];
figure;
hold on;
x = 0:1/1000:2*pi;
y_1 = sigma*cos(x - theta_s) + sqrt(1 + Delta*cos(2*x));
y_2 = sigma*cos(x - theta_s) - sqrt(1 + Delta*cos(2*x));
plot(x,y_1,'black');
plot(x,y_2,'black');
grid on;

%% Plot theta if real, if positive, if 1st derivative is ~zero, and if 2nd derivative is positive
for j=1:numel(theta);
    A = isreal(theta(j));
    x_j = theta(j);
    y_j = sigma*cos(x_j - theta_s) + sqrt(1 + Delta*cos(2*x_j));
    FirstDer = sigma* sin(theta(j) - theta_s) + Delta*sin(2*theta(j))/...
        sqrt(1 + Delta*cos(2*theta(j)));
    SecDer = -sigma*cos(theta(j)-theta_s) - 2*Delta*cos(2*theta(j))/...
        (1 + Delta*cos(2*theta(j)))^(1/2) - Delta^2 * (sin(2*theta(j)))^2/...
        (1 + Delta*cos(2*theta(j)))^(3/2);
    if  A == 1 && x_j>=0 && FirstDer < 1E-7 && SecDer > 0
        plot(x_j,y_j,['o','blue'])
    end
end
格式长
θ=0.77944100;
西格玛=0.50659500;
δ=0.52687700;
%%定义四次多项式的系数
α=cos(2*θs);
β=sin(2*theta_s);
γ=2*δ^2/σ^2;
a=-gamma^2-β^2*δ^2-α^2*δ^2+2*α*δ*γ;
b=2*alpha*gamma-2*Delta*gamma-2*alpha^2*Delta+2*alpha*Delta^2-。。。
2*β^2*δ;
c=2*gamma^2-2*alpha*Delta*gamma-2*gamma-alpha^2+4*alpha*Delta+。。。
β^2*δ^2-β^2-δ^2;
d=-2*alpha*gamma+2*Delta*gamma+2*alpha+2*beta^2*Delta-2*Delta;
e=β^2-γ^2+2*γ-1;
%%数值求解多项式。
P=[a b c d e];
R=根(P);
%%求解x的r=cos(2x):x=n*pi+-1/2*acos(r)。使用n=0和1。
θ=[1/2.*acos(R)-1/2.*acos(R)pi+1/2.*acos(R)pi-1/2.*acos(R)];
图形
等等
x=0:1/1000:2*pi;
y_1=sigma*cos(x-θs)+sqrt(1+Delta*cos(2*x));
y_2=sigma*cos(x-θ-s)-sqrt(1+Delta*cos(2*x));
图(x,y_1,'black');
图(x,y_2,'black');
网格化;
%%如果为实,如果为正,如果一阶导数为零,如果二阶导数为正,则绘制θ
对于j=1:numel(θ);
A=以色列(θ(j));
x_j=θ(j);
y_j=sigma*cos(x_j-θs)+sqrt(1+Delta*cos(2*x_j));
FirstDer=sigma*sin(θ(j)-theta_s)+Delta*sin(2*theta(j))/。。。
sqrt(1+Delta*cos(2*theta(j));
SecDer=-sigma*cos(θ(j)-theta_s)-2*Delta*cos(2*theta(j))/。。。
(1+Delta*cos(2*theta(j))^(1/2)-Delta^2*(sin(2*theta(j)))^2/。。。
(1+Delta*cos(2*theta(j))^(3/2);
如果A==1&&x_j>=0&&FirstDer<1E-7&&SecDer>0
绘图(x_j,y_j,['o','blue'])
结束
结束

完成所有打印后,获取轴手柄:

ax = gca;
然后写:

X = get(ax.Children,{'XData'});
X
将是图形中所有线的所有X轴值的单元格数组。每行一个单元格

对于上述代码:

X = 
    [1.961054062875753]
    [4.514533853417446]
    [1x6284 double]
    [1x6284 double]
(首先,代码都成功了。感谢您的努力。)

这里有很多选择。下面有一对夫妇

在生成值时记录这些值

在“success”
if
语句中,只需记录这些值。请参见下面对代码的编辑

这将永远是我的首选,它似乎更有效

xyResults = zeros(0,2);  %%% INITIALIZE HERE
for j=1:numel(theta);
    A = isreal(theta(j));
    x_j = theta(j);
    y_j = sigma*cos(x_j - theta_s) + sqrt(1 + Delta*cos(2*x_j));
    FirstDer = sigma* sin(theta(j) - theta_s) + Delta*sin(2*theta(j))/...
        sqrt(1 + Delta*cos(2*theta(j)));
    SecDer = -sigma*cos(theta(j)-theta_s) - 2*Delta*cos(2*theta(j))/...
        (1 + Delta*cos(2*theta(j)))^(1/2) - Delta^2 * (sin(2*theta(j)))^2/...
        (1 + Delta*cos(2*theta(j)))^(3/2);
    if  A == 1 && x_j>=0 && FirstDer < 1E-7 && SecDer > 0
        xyResults(end+1,:) = [x_j  y_j];  %%%% RECORD HERE
        plot(x_j,y_j,['o','blue'])
    end
end

绘图
。将其存储到一个数组中,然后您可以稍后访问该数组。或者,只需将数据存储在与绘图相同的
if
语句中即可。太棒了,第一个示例是我在脑海中设想的,但无法做到的!不过,接下来的问题是:在生成的图中,您将看到一个最小值是局部的,另一个是全局的。如果我只想把x和y作为全局最小值,我可以用“min(y_j)”只得到相关的xysults行吗?
%First find the objects witht the data you want
%     (Ideally you could record handles to the lines as you generated
%      them above. But then you could also simply record the answer, so
%      let's assume that direct record is not possible.)
%     (BTW, 'findobj' is an underused, powerful function.)
h = findobj(0,'Marker','o','Color','b','type','line')

%Then get the `xdata` filed from each
capturedXdata = get(h,'XData');
capturedXdata =
  2×1 cell array
    [1.96105406287575]
    [4.51453385341745]

%Then get the `ydata` filed from each
capturedYdata = get(h,'YData');
capturedYdata =
  2×1 cell array
    [1.96105406287575]
    [4.51453385341745]