Writing code to calculate a function for any user input array
3 views (last 30 days)
Show older comments
I am working on a script to calculate the efficiency of a propellor at multiple conditions. I have written a function which I have confirmed works as intended however I have run into a roadblock in my attempt to run the function for user input variables. My script calls upon the user to input the variables below
PA, n, d, vt, hp, and isa.
I would like the user to be able to input any size array for each of these varaibles so as to generate a propellor efficiency # for all possible combintations of the variables entered. For example if the user input 3 values for PA, 3 values for vt, and 3 values for hp they would get 9 results back reflecting every combination of the aforementioned variables. I have begun by attempting to make this function with only two varaibles (n, and vt), however the code ceases to function properly if i input more than two values of n. The current version of this part of the code is below.
%Calculating Prop Efficiency (np) across all user given parameters
i=1;%vt counter
j=1;%PA counter
k=1;%(prop rpm counter)
l=1;%(isa counter)
jj=1;%(prop dia counter)
kk=1;%hp counter
ll=1%np counter
while ll<(numel(n)*numel(vt))%Overarching index for Propellor efficiency
for i=1:numel(vt)%Calculate Prop Efficiency at 1st RPM for all Velocities
i
np(ll)=calculatenpf(PropMap5,PropMap6,PropMap7,PropMap8,PropMap9,PropMap95,PropMap98,PA(j),n(k),isa(l),d(jj),hp(kk),vt(i))
i=i+1
ll=ll+1
end
if k<numel(n)%Check if prop RPM Values are still remaining
k=k+1
i=1
continue
end
for i=1:numel(vt)% Calculate Propellor Efficiency at second RPM
i
k
np(ll)=calculatenpf(PropMap5,PropMap6,PropMap7,PropMap8,PropMap9,PropMap95,PropMap98,PA(j),n(k),isa(l),d(jj),hp(kk),vt(i))
i=i+1
ll=ll+1
end
end
1 Comment
dpb
on 7 Jul 2023
The problem is really not at the upper level but in the calculation function -- it should be vectorized to handle array inputs. See examples at ndgrid -- although it only used 2D, the same idea works for higher dimensions as well.
Also, as a syntax issue, having a sequence of sequentially-named variables is a pretty good sign are not using MATLAB vectorized operations effectively -- these should almost certainly also be arrays.
But, we don't have enough of the details to progress beyond generalities...
Answers (1)
Voss
on 7 Jul 2023
How about something like this?
Nvt = numel(vt);
NPA = numel(PA);
Nn = numel(n);
Nisa = numel(isa);
Nd = numel(d);
Nhp = numel(hp);
ll = 1; %np counter
for i = 1:Nvt %vt counter
for j = 1:NPA %PA counter
for k = 1:Nn %(prop rpm counter)
for l = 1:Nisa %(isa counter)
for jj = 1:Nd %(prop dia counter)
for kk = 1:Nhp %hp counter
np(ll)=calculatenpf(PropMap5,PropMap6,PropMap7,PropMap8,PropMap9,PropMap95,PropMap98,PA(j),n(k),isa(l),d(jj),hp(kk),vt(i));
ll = ll+1;
end
end
end
end
end
end
Or instead of np being a vector, you can make it a 6-dimensional array, which may make it easier to keep track of which parameter values went into calculating each element of np:
Nvt = numel(vt);
NPA = numel(PA);
Nn = numel(n);
Nisa = numel(isa);
Nd = numel(d);
Nhp = numel(hp);
np = zeros(Nvt,NPA,Nn,Nisa,Nd,Nhp);
for i = 1:Nvt %vt counter
for j = 1:NPA %PA counter
for k = 1:Nn %(prop rpm counter)
for l = 1:Nisa %(isa counter)
for jj = 1:Nd %(prop dia counter)
for kk = 1:Nhp %hp counter
np(i,j,k,l,jj,kk)=calculatenpf(PropMap5,PropMap6,PropMap7,PropMap8,PropMap9,PropMap95,PropMap98,PA(j),n(k),isa(l),d(jj),hp(kk),vt(i));
end
end
end
end
end
end
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!