Search code examples
matlabscilab

Inconsistent row/column dimensions error in Scilab


I want to plot limacon in scilab, I've got this equations to process:

equations

I know that r>0 and l>0

When I compile the below code, I get this error at line 5:

Inconsistent row/column dimensions.

If I set t to a specific number, I end up with clean plot, no function in it.

I tried to change r and l to other numbers, but this doesn't do anything. Anyone have an idea what I'm doing wrong?

r=1;
l=1;
t=linspace(0,2,10);
x = 2 * r * (cos(t))^2 + l * cos(t);
y = 2 * r * cos(t) * sin(t) + l * sin(t);
plot (x,y);

Solution

  • You're (accidentally) trying to do matrix multiplication with *.

    Instead, you need element-wise multiplication with .* (Scilab docs, MATLAB docs).

    Similarly, you should be using the element-wise power .^ to square the cosine term in the first equation.

    See the comments in the amended code below...

    r = 1;
    l = 1;
    % Note that t is an array, so we might encounter matrix operations!
    t = linspace(0,2,10); 
    % Using * on the next line is fine, only ever multiplying scalars with the array.
    % Could equivalently use element-wise multiplication (.*) everywhere to be explicit.
    % However, we need the element-wise power (.^) here for the same reason!
    x = 2 * r * (cos(t)).^2 + l * cos(t);      
    % We MUST use element-wise multiplication for cos(t).*sin(t), because the dimensions
    % don't work for matrix multiplication (and it's not what we want anyway).
    % Note we can leave the scalar/array product l*sin(t) alone, 
    % or again be explicit with l.*sin(t) 
    y = 2 * r * cos(t) .* sin(t) + l * sin(t);
    plot (x,y);