函数 contour 在 Scilab 中的输出与在 MATLAB 中的输出不同?
The function contour has different outputs in Scilab than in MATLAB?
我正在尝试将此 Matlab 代码转换为 Scilab,但我遇到了一些问题。
clear all; clc;
c0 = 0.8;
c1 = 1.3;
c11 = -6.1;
c12 = -0.6;
c2 = 1.7;
c22 = -1.7;
g = 0.05;
d = 0.01;
x1 = -9;
x2 = 8;
k = 1;
kmax = 100;
x1trace = [x1];
x2trace = [x2];
i = 2;
while k < kmax
gr1 = c1 + c12*x2 + 2*c11*x1;
x1 = x1 + g*gr1
x1trace(i) = x1;
x2trace(i) = x2;
i = i + 1;
gr2 = c2 + c12*x1 + 2*c22*x2;
x2 = x2 + g*gr2
x1trace(i) = x1;
x2trace(i) = x2;
i = i + 1;
if sqrt(gr1^2 + gr2^2) <= d;
break;
end
k = k + 1;
end
x = -10:0.1:10;
y = -10:0.1:10;
[X, Y] = meshgrid(x, y);
Z = c0 + c1*X + c2*Y + c12*X.*Y + c11*X.^2 + c22*Y.^2;
[C, h] = contour(X, Y, Z);
clabel(C, h);
hold on;
plot(x1trace, x2trace, '-');
text(x1trace(1) + 0.2, x2trace(1) + 0.5, 'M0');
text(x1 + 2, x2, ...
strvcat(['x1 = ' (num2str(x1))], ...
['x2 = ' (num2str(x2))], ...
['k = ' (num2str(k))]));
hold off;
我收到此行的错误:
[C, h] = contour(X, Y, Z);
Wrong number of output arguments.
我应该更改什么来修复它?代码中是否还有其他错误?
您应该具有与以下 Scilab 代码大致相同的渲染效果。 Scilab 的 contour
需要(在第 4 个参数中)水平曲线的数量或值本身。此外,您应该使用 ndgrid
而不是 meshgrid
:
clear clc;
c0 = 0.8;
c1 = 1.3;
c11 = -6.1;
c12 = -0.6;
c2 = 1.7;
c22 = -1.7;
g = 0.05;
d = 0.01;
x1 = -9;
x2 = 8;
k = 1;
kmax = 100;
x1trace = [x1];
x2trace = [x2];
i = 2;
while k < kmax
gr1 = c1 + c12*x2 + 2*c11*x1;
x1 = x1 + g*gr1
x1trace(i) = x1;
x2trace(i) = x2;
i = i + 1;
gr2 = c2 + c12*x1 + 2*c22*x2;
x2 = x2 + g*gr2
x1trace(i) = x1;
x2trace(i) = x2;
i = i + 1;
if sqrt(gr1^2 + gr2^2) <= d;
break;
end
k = k + 1;
end
x = -10:0.1:10;
y = -10:0.1:10;
[X, Y] = ndgrid(x, y);
Z = c0 + c1*X + c2*Y + c12*X.*Y + c11*X.^2 + c22*Y.^2;
clf
gcf.color_map=parulacolormap(8)($:-1:1,:)
contour(x, y, Z,0:-100:-700);
plot(x1trace, x2trace, '-');
xstring(x1trace(1) + 0.2, x2trace(1) + 0.5, 'M0');
xstring(x1 + 2, x2, ...
['x1 = '+string(x1)
'x2 = '+string(x2)
'k = '+string(k)]);
我正在尝试将此 Matlab 代码转换为 Scilab,但我遇到了一些问题。
clear all; clc;
c0 = 0.8;
c1 = 1.3;
c11 = -6.1;
c12 = -0.6;
c2 = 1.7;
c22 = -1.7;
g = 0.05;
d = 0.01;
x1 = -9;
x2 = 8;
k = 1;
kmax = 100;
x1trace = [x1];
x2trace = [x2];
i = 2;
while k < kmax
gr1 = c1 + c12*x2 + 2*c11*x1;
x1 = x1 + g*gr1
x1trace(i) = x1;
x2trace(i) = x2;
i = i + 1;
gr2 = c2 + c12*x1 + 2*c22*x2;
x2 = x2 + g*gr2
x1trace(i) = x1;
x2trace(i) = x2;
i = i + 1;
if sqrt(gr1^2 + gr2^2) <= d;
break;
end
k = k + 1;
end
x = -10:0.1:10;
y = -10:0.1:10;
[X, Y] = meshgrid(x, y);
Z = c0 + c1*X + c2*Y + c12*X.*Y + c11*X.^2 + c22*Y.^2;
[C, h] = contour(X, Y, Z);
clabel(C, h);
hold on;
plot(x1trace, x2trace, '-');
text(x1trace(1) + 0.2, x2trace(1) + 0.5, 'M0');
text(x1 + 2, x2, ...
strvcat(['x1 = ' (num2str(x1))], ...
['x2 = ' (num2str(x2))], ...
['k = ' (num2str(k))]));
hold off;
我收到此行的错误:
[C, h] = contour(X, Y, Z);
Wrong number of output arguments.
我应该更改什么来修复它?代码中是否还有其他错误?
您应该具有与以下 Scilab 代码大致相同的渲染效果。 Scilab 的 contour
需要(在第 4 个参数中)水平曲线的数量或值本身。此外,您应该使用 ndgrid
而不是 meshgrid
:
clear clc;
c0 = 0.8;
c1 = 1.3;
c11 = -6.1;
c12 = -0.6;
c2 = 1.7;
c22 = -1.7;
g = 0.05;
d = 0.01;
x1 = -9;
x2 = 8;
k = 1;
kmax = 100;
x1trace = [x1];
x2trace = [x2];
i = 2;
while k < kmax
gr1 = c1 + c12*x2 + 2*c11*x1;
x1 = x1 + g*gr1
x1trace(i) = x1;
x2trace(i) = x2;
i = i + 1;
gr2 = c2 + c12*x1 + 2*c22*x2;
x2 = x2 + g*gr2
x1trace(i) = x1;
x2trace(i) = x2;
i = i + 1;
if sqrt(gr1^2 + gr2^2) <= d;
break;
end
k = k + 1;
end
x = -10:0.1:10;
y = -10:0.1:10;
[X, Y] = ndgrid(x, y);
Z = c0 + c1*X + c2*Y + c12*X.*Y + c11*X.^2 + c22*Y.^2;
clf
gcf.color_map=parulacolormap(8)($:-1:1,:)
contour(x, y, Z,0:-100:-700);
plot(x1trace, x2trace, '-');
xstring(x1trace(1) + 0.2, x2trace(1) + 0.5, 'M0');
xstring(x1 + 2, x2, ...
['x1 = '+string(x1)
'x2 = '+string(x2)
'k = '+string(k)]);