Home > Back-end >  The function contour has different outputs in Scilab than in MATLAB?
The function contour has different outputs in Scilab than in MATLAB?

Time:05-24

I'm trying to convert this Matlab code to Scilab, but I have some problems.

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;

I get an error for this line:

[C, h] = contour(X, Y, Z);
Wrong number of output arguments.

What should I change to fix it? Are there also any other errors in the code ?

CodePudding user response:

You should have roughly the samed rendering with the following Scilab code. Scilab's contour needs (in the 4th argument) the number of level curves or the values themselves. In addition, you should use ndgrid instead of 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)]); 

enter image description here

  • Related