Why is my code showing output as an array instead of single value?

3 views (last 30 days)
Hello, I am new to MATLAB, I have written a code where I want to find the value of A for the given inputs, but the value is being represented as an array, can anyone help?
Here is the code
%A check for different hydrofoil span
I = input("Body moment of inertia? in kgm^2");
l = input("Length of body? in m ");
b = input("Breadth of body? in m ");
w = input("Width of body? in m ");
d = input("diameter? in m ");
h = input("input length of cylindrical cable? in m ");
fl = input("Length of fin? in m ");
fb = input("Breadth of fin? in m ");
lf = input("Length of application of fin force? in m ");
bf = input("Breadth of application of fin force? in m ");
lc = input("Length of application of cable force? in m ");
bc = input("breadth of application of cable force? in m ");
lt = input("Length of application of towing force? in m");
bt = input("Breadth of application of towing force? in m");
xcm = input("center of gravity x?");
ycm = input("center of gravity y?");
beta = input("angle between position vector and fin lift? in deg ");
AR = fl/fb;
Cdc = input("Drag coefficient cable? ");
%Clc = input("Lift coefficient cable? ");
Cd = input("Drag coefficient body? ");
Cl = input("Lift coefficient body? ");
%Cdf = input("Drag coefficient fin? ");
Clf = 1/((1/(2*pi*beta)+(1/(pi*AR)+(1/(2*pi*(AR^2)))))); %Lift coefficient of fin?
rhow = input("Density? in kg/m^3 ");
rhob = input("Body density?");
v = input("Towing speed? in m/s ");
pi = 3.14;
Vb = l*b*w;
Mb = rhob*Vb; %Body mass
Mwb = rhow*Vb;
Vf = fl*fb*t;
Mf = rhob*Vf; %fin mass
Mwf = rhow*Vf;
Vc = pi*((d/2)^(2))*h;
Mc = rhob*Vc; %towed array mass
Mwc = rhow*Vc;
B = (Mwb+2*Mwf+2*Mwc)*9.81;
W = (Mb+2*Mf+2*Mc)*9.81;
Dc = 0.5*Cdc*rhow*(v^2)*(2*pi*(d/2)*h);
%Lc = 0.5*Clc*rhow*(v^2)*(2*pi*(d/2)*h);
Db = 0.5*Cd*rhow*(v^2)*l*b;
Lb = 0.5*Cl*rhow*(v^2)*w*b;
Lf = 0.5*Clf*rhow*(v^2)*fl*fb;
%Df = 0.5*Cdf*rhow*(v^2)*fl*fb;
theta = atand((Lb+2*Lf+B-W)/(Db+2*Dc)); %angle between horizontal and T? in deg
T = (2*Dc+Db)/cosd(theta);
A = (-2*Dc*bc +2*Lf*lf + (B-W)*ycm - T*lt*sind(theta) - T*bt*cosd(theta))/I;
The output theta, T and A are represented as an array
theta =
Columns 1 through 20
47.5198 45.9120 44.2053 42.3936 40.4710 38.4317 36.2704 33.9824 31.5644 29.0142 26.3316 23.5188 20.5805 17.5247 14.3624 11.1083 7.7800 4.3981 0.9853 -2.4346
Columns 21 through 40
-5.8372 -9.1990 -12.4982 -15.7153 -18.8341 -21.8412 -24.7271 -27.4851 -30.1116 -32.6057 -34.9682 -37.2020 -39.3110 -41.3001 -43.1750 -44.9414 -46.6054 -48.1732 -49.6506 -51.0435
Columns 41 through 60
-52.3574 -53.5975 -54.7689 -55.8762 -56.9238 -57.9156 -58.8556 -59.7472 -60.5936 -61.3978 -62.1627 -62.8907 -63.5844 -64.2458 -64.8770 -65.4798 -66.0561 -66.6074 -67.1352 -67.6410
Columns 61 through 80
-68.1259 -68.5912 -69.0380 -69.4673 -69.8801 -70.2773 -70.6596 -71.0280 -71.3831 -71.7256 -72.0561 -72.3753 -72.6837 -72.9817 -73.2700 -73.5489 -73.8189 -74.0804 -74.3338 -74.5795
Columns 81 through 100
-74.8178 -75.0489 -75.2733 -75.4912 -75.7029 -75.9086 -76.1086 -76.3031 -76.4923 -76.6765 -76.8558 -77.0304 -77.2005 -77.3663 -77.5279 -77.6855 -77.8392 -77.9892 -78.1356 -78.2785
Can anyone please help why am I getting an array instead of single output value.
Thanks in advance.
  4 Comments
Image Analyst
Image Analyst on 7 Dec 2022
No they're not. For example with this line
I = input("Body moment of inertia? in kgm^2");
nowhere do you say what to enter there.
Also, the code doesn't run. What value are you assigning for (lower case) t in
Vf = fl*fb*t;
I don't see t assigned anywhere in the code.

Sign in to comment.

Answers (1)

Torsten
Torsten on 7 Dec 2022
If your inputs and the variable t are scalar values, A cannot be an array.
My guess is that you specified t as a vector.
%A check for different hydrofoil span
I = 1;%input("Body moment of inertia? in kgm^2");
l = 1;%input("Length of body? in m ");
b = 1;%input("Breadth of body? in m ");
w = 1;%input("Width of body? in m ");
d = 1;%input("diameter? in m ");
h = 1;%input("input length of cylindrical cable? in m ");
fl = 1;%input("Length of fin? in m ");
fb = 1;%input("Breadth of fin? in m ");
lf = 1;%input("Length of application of fin force? in m ");
bf = 1;%input("Breadth of application of fin force? in m ");
lc = 1;%input("Length of application of cable force? in m ");
bc = 1;%input("breadth of application of cable force? in m ");
lt = 1;%input("Length of application of towing force? in m");
bt = 1;%input("Breadth of application of towing force? in m");
xcm = 1;%input("center of gravity x?");
ycm = 1;%input("center of gravity y?");
beta = 1;%input("angle between position vector and fin lift? in deg ");
AR = fl/fb;
Cdc = 1;%input("Drag coefficient cable? ");
%Clc = 1;%input("Lift coefficient cable? ");
Cd = 1;%input("Drag coefficient body? ");
Cl = 1;%input("Lift coefficient body? ");
%Cdf = 1;%input("Drag coefficient fin? ");
Clf = 1/((1/(2*pi*beta)+(1/(pi*AR)+(1/(2*pi*(AR^2)))))); %Lift coefficient of fin?
rhow = 1;%input("Density? in kg/m^3 ");
rhob = 1;%input("Body density?");
v = 1;%input("Towing speed? in m/s ");
t = 1;
pi = 3.14;
Vb = l*b*w;
Mb = rhob*Vb; %Body mass
Mwb = rhow*Vb;
Vf = fl*fb*t;
Mf = rhob*Vf; %fin mass
Mwf = rhow*Vf;
Vc = pi*((d/2)^(2))*h;
Mc = rhob*Vc; %towed array mass
Mwc = rhow*Vc;
B = (Mwb+2*Mwf+2*Mwc)*9.81;
W = (Mb+2*Mf+2*Mc)*9.81;
Dc = 0.5*Cdc*rhow*(v^2)*(2*pi*(d/2)*h);
%Lc = 0.5*Clc*rhow*(v^2)*(2*pi*(d/2)*h);
Db = 0.5*Cd*rhow*(v^2)*l*b;
Lb = 0.5*Cl*rhow*(v^2)*w*b;
Lf = 0.5*Clf*rhow*(v^2)*fl*fb;
%Df = 0.5*Cdf*rhow*(v^2)*fl*fb;
theta = atand((Lb+2*Lf+B-W)/(Db+2*Dc)); %angle between horizontal and T? in deg
T = (2*Dc+Db)/cosd(theta);
A = (-2*Dc*bc +2*Lf*lf + (B-W)*ycm - T*lt*sind(theta) - T*bt*cosd(theta))/I
A = -7.2800
  3 Comments
Torsten
Torsten on 7 Dec 2022
The t in
Vf = fl*fb*t;
must be a scalar.
I guess that it's an array in your code (you didn't specify it).

Sign in to comment.

Categories

Find more on Programming 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!