structure inside a function

Hey all!
I am having a ton of issues with this line of code. So I have this equation in a function in which I have made the structure "inputs" with the different aircraft properties. in a separate script, I have called the function and labeled again the inputs from my function page. Along with this, I have named my vmax.
I keep getting the same error message of too many input arguments. What am I getting wrong?
D = (0.5*rho0*((vmax*1.687810)^2)*inputs.S*inputs.Cd0)+(((inputs.WS*inputs.S)^2)/(0.5*rho0*((vmax*1.687810)^2)*inputs.E*inputs.AR*inputs.S*pi));

9 个评论

Can you show other parts of the code? Where are you creating the struct? How are you passing it to this function?
Why did you hide the function names?
We cannot help you unless more details are given.
Also don't atatch your code as image snippet..copy and paste here.
As you are suing inputs.S etc..ine formula...it is going to be an array....when you have array, you need to use element by element operations.
This is the function page ( hid name for privacy but realize my name is on the account haha thank you for pointing that out)
function [V,D,P] = hw_dragPower_sharmaAshlianne(vmax, inputs)
% Function will return stall velocity, drag force, and power
% when given max velocity and aircraft properties are inputed,
% INPUTS:
% Maximum velocity(v_max) [m/s]
% aircraft properties [listed as a structure below]
% OUTPUTS:
% V: velocity vector from the stall velocity up to the maximum velocity [m/s]
% D: Drag vector [lbf]
% P: Power Vector [HP]
% KNOWNS:
T0 = 288.16; % [K] temp at sea level
rho0 = 0.0023765; % [slugs/ft^3] deinsity at sea level
R = 287; % [J/(kg k)] Gas constant of air
% list the properties in structures
% Baseline Aircraft Properties
inputs(1).WS = 6.9; %[lbf/ft^2] wing loading
inputs(1).AR = 6.5; % Aspect Ratio
inputs(1).Cd0 = 0.0373; % Parasite drag coefficient
inputs(1).E = 0.81; % Span efficiency
inputs(1).CLmax = 1.5; % Clean Maximum Lift
inputs(1).S = 179; % [ft^2] wing reference area
% Landing Configuration aircraft Properties
inputs(2).WS = 6.9; %[lbf/ft^2] wing loading
inputs(2).AR = 6.5; % Aspect Ratio
inputs(2).Cd0 = 0.042; % Parasite drag coefficient
inputs(2).E = 0.81; % Span efficiency
inputs(2).CLmax = 2.0; % Clean Maximum Lift
inputs(2).S = 179; % [ft^2] wing reference area
% Heavy wing loading aircraft
inputs(3).WS = 7.4; %[lbf/ft^2] wing loading
inputs(3).AR = 6.5; % Aspect Ratio
inputs(3).Cd0 = 0.0373; % Parasite drag coefficient
inputs(3).E = 0.81; % Span efficiency
inputs(3).CLmax = 1.5; % Clean Maximum Lift
inputs(3).S = 179; % [ft^2] wing reference area
% value for vmax
vmax = 85; % knots
% Naming output equations
D = (0.5*rho0*((vmax*1.687810)^2)*179*inputs.Cd0)+(((inputs.WS*179)^2)/(0.5*rho0*((vmax*1.687810)^2)*0.81*6.5*179*pi)); % [lbf]
P = D*(vmax*1.687810); % [HP] Power
V = ((inputs.WS*inputs.S)/(0.5*rho0*inputs.S*inputs.CLmax))^(0.5); % [m/s] Maximum stall velocity
end
script where I call my function
% HOUSEKEEPING
clc % Clear command window
clear % Clear variables in workspace
close all % Close any and all open figures
% Drag and Power (hw 2)
vmax = 85;
% list the properties in structures
% Baseline Aircraft Properties
inputs(1).WS = 6.9; %[lbf/ft^2] wing loading
inputs(1).AR = 6.5; % Aspect Ratio
inputs(1).Cd0 = 0.0373; % Parasite drag coefficient
inputs(1).E = 0.81; % Span efficiency
inputs(1).CLmax = 1.5; % Clean Maximum Lift
inputs(1).S = 179; % [ft^2] wing reference area
[V,D,P] = hw_dragPower_sharmaAshlianne(vmax, inputs);
outputs = [V,D,P];
I just removed some of the structures from my formula to see if that would reduve the inputs (bc my error code said I had too many)
Can you explain what you mean by element by element?
"Can you explain what you mean by element by element?"
Most likely you should be using array operations (i.e. element-by-element), not matrix operations:
I hear you, I looked over that page, thank you. However, I am specifically asked to use a structure. I am really concerned about my equation having too many input values.
Stephen23
Stephen23 2020-10-12
编辑:Stephen23 2020-10-12
"I am really concerned about my equation having too many input values."
As Ameer Hamza explained, the cause of the error is your inadvertent use of comma-separated lists generated from a non-scalar structure:
Why do you define inputs as an input to the function hw_dragPower_sharmaAshlianne but then overwrite this with data inside the function? What is the point of that?
Just as I have commented below, this is the whole error message in my command window.
>> hw_dragPower_sharmaAshlianne(vmax, inputs)
Error using *
Too many input arguments.
Error in hw_dragPower_sharmaAshlianne (line 50)
D =
(0.5*rho0*((vmax*1.687810)^2)*179*inputs.Cd0)+(((inputs.WS.*inputs.S)^2)/(0.5*rho0*((vmax*1.687810)^2)*inputs.E.*inputs.AR.*inputs.S.*pi));
% [lbf]

请先登录,再进行评论。

 采纳的回答

Ameer Hamza
Ameer Hamza 2020-10-12
编辑:Ameer Hamza 2020-10-12
In your code 'inputs' is a 3-element array of struct. When you write
inputs.Cd0
It returns 3 values. If you want to multiply all three values with a number then you need to write it like this
a*[inputs.Cd0]; % 'a' is used as an example here.
In that case, you may also need to use element-wise operators: https://www.mathworks.com/help/matlab/matlab_prog/array-vs-matrix-operations.html

6 个评论

oh, So I am meant to use brackets instead of nothing around the "inputs.Cd0"?
This is now the message I am getting in my command window.
>> hw_dragPower_sharmaAshlianne(vmax, inputs)
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the
number of rows in the second matrix. To perform elementwise multiplication, use '.*'.
Error in hw_dragPower_sharmaAshlianne (line 50)
D =
(0.5*rho0*((vmax*1.687810)^2)*179*[inputs.Cd0])+((([inputs.WS]*[inputs.S])^2)/(0.5*rho0*((vmax*1.687810)^2)*[inputs.E]*[inputs.AR]*[inputs.S]*pi));
% [lbf]
Yes, you need to use brackets. And I already mentioned that now you will need to use element-wise operators. For example
D = (0.5.*rho0.*((vmax*1.687810).^2).*[inputs.S].*[inputs.Cd0])+((([inputs.WS].*[inputs.S]).^2)./(0.5.*rho0.*((vmax.*1.687810).^2).*[inputs.E].*[inputs.AR].*[inputs.S].*pi));
Also make same changes in this line
V = (([inputs.WS].*[inputs.S])./(0.5.*rho0.*[inputs.S].*[inputs.CLmax])).^(0.5);
For all of you that are helping me, you all are the smartest people ive ever talked to!
Ameer Hamza, sir, I think the way you explained this really helped! Now I understand that you must use brackets when using structure values in a function. You all taught me that the extra period goes between structures when multiplying or dividing the two (but not INSIDE the brackets) that is where I found one of my mistakes. Thanks to you all, I was able to fix my V equation aswell.
I just need to learn how to display the three output values as a vector. Thank you all so much for your help. I think you saved me from a mental breakdown.
I am glad to be of help! :)
You can display the outputs as 3 sepeate vectors instead of combining in a single array
disp(V);
disp(D);
disp(P);
or you can combine them as column vectors in the matrix
outputs = [V.' D.' P .'];

请先登录,再进行评论。

更多回答(1 个)

KSSV
KSSV 2020-10-12
编辑:KSSV 2020-10-12
If you have two equal vectors, like:
a = rand(1,3) ;
b = rand(1,3) ;
m = a.*b % this should be used. This iscalled element by element multiplication
d = a./b % element by element divison
p = a*b % this will trhow error.
l = a/b % this is not desired
In your case: Try the below. If any error, try using .*, ./ at vectors multiplication and divison.
D = (0.5*rho0*((vmax*1.687810)^2)*inputs.S.*inputs.Cd0)+(((inputs.WS.*inputs.S).^2)./(0.5*rho0*((vmax*1.687810)^2)*inputs.E.*inputs.AR.*inputs.S*pi));

2 个评论

I am still getting that there are too many input arguments..
I tried using the input.WS.
and I tried [input.WS.]
and [input.WS]
all of which gave me the same error code of too many input arguments
Try like this. If not working check the size of each S, Cd0, WS etc.....If still there is a error. Share your code. There should be some other problem.
S = [input.S] ;
Cd0 = [inputs.Cd0] ;
WS = [inputs.WS] ;
E = inputs.E] ;
AR = [inputs.AR] ;
D = (0.5*rho0*((vmax*1.687810)^2)*S.*Cd0)+(((WS.*S).^2)./(0.5*rho0*((vmax*1.687810)^2)*E.*AR.*S*pi));

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Programming 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by