Search code examples
matlabsym

Count number of unique symbols for a Buckingham Pi calculation in an expression with MATLAB


I am trying to write a script that goes through the Buckingham Pi theorem given a list of variables, each having a dimension. The set of dimensions do not need to be unique (it can contain repeats) only the same size as the set of variables.

clc, clear, close all

syms M L T Theta
dimen = [M,L,T,Theta]
  • mass - M
  • length - L
  • time - T
  • temperature - Theta

(I can add in other dimensions like electric current later, but for now, I want to get it working with these four).

Here is what I have so far in MatLab.

A = L^2;    % maybe an area
V = L/T;    % maybe a velocity
D = M/L^3;  % maybe a density
% This the array of the combinations
param = {A,V,D};

I want to count how many of the syms M L T and Theta show up in my cell param. For example starting at the first entry in the cell array.

param{1} = A
L^2

At this step, it should count that L has shown up once, and the others 0 times each.

param{2} = V
L/T

At this step, it counts that L has shown up once, but since it was already counted so I don't want to count it again. It should also count that T has shown up once. So far 1 L, and 1 T.

param{3} = D
M/L^3

Finally, this count that M has shown up once. So far 1 L, 1 T, and 1 M. Since there are four possible symbols, I want to end the algorithm with this.

j = num; % how many times each of the syms was counted at least once.

If a dimension is not counted, that is fine. I am only interested in counting how many times each of the dimensions in the cell array are counted at least once. I will then use these to solve a system of equations to identify dimensionless groups.

I've received some suggestions based on other answers that I am providing below. How to extract powers of a symbolic polynomial?

present = cellfun(@(expr), ismember(dimen, symvar(expr)), param, 'UniformOutput', false)
counts = sum(vertcat(present{:}), 1)

This last suggestions gives this error.

Error using cellfun

Input #2 expected to be a cell array, was sym instead.

Addendum

Removing the comma as suggested in the comments/answer still gives the same error. I am using release 2021b and a mlx file. Error without comma


Solution

  • cellfun expects a cell array as its second argument: by introducing a comma between @(expr) and ismember(dimen, symvar(expr)) it's as if you were asking cellfun to iterate over the content of ismember(dimen, symvar(expr)), which is not what you really want, as that's the body of the anonymous function you are passing to cellfun as first argument.

    The correct way of using cellfun is shown in the following script:

    clc, clear, close all
    syms M L T Theta
    dimen = [M,L,T,Theta]
    A = L^2;    % maybe an area
    V = L/T;    % maybe a velocity
    D = M/L^3;  % maybe a density
    % This the array of the combinations
    param = {A,V,D};
    
    % Counts the appearences of each dimension in each param
    % and stores them in a cell array of vectors
    present = cellfun(@(expr) ismember(dimen, symvar(expr)), param, 'UniformOutput', false)
    
    % Unpack the cell array of vectors and
    % compute the total number of appearences of each dimension
    counts = sum(vertcat(present{:}), 1)