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.