Calculating with Methods from Classes

8 views (last 30 days)
Leon Stolp
Leon Stolp on 31 Mar 2020
Commented: Leon Stolp on 1 Apr 2020
Hi,
I'm currently getting into classes and methods, due to a project I'm working on. I have a function which needs a whole lot of other funcions to calculate it's variables.
Until now i made a Class containing all the functions I need. Now I want to give the Class the needed Properties and want it to automatically run every function in the Class and print out the final result.
I have quite a hard time finding a solution for that because I don't really now how/where to start. To begin with, I don't even know if classes are able to do what I want.
Has anybody some advice? It would be greatly appreciated!
Greetings,
Leon
  2 Comments
Adam
Adam on 31 Mar 2020
is the best source for class-based help and information.
In terms of your question it is a bit hard to answer with so little context. Giving properties to a class is as easy as simply setting them, e.g.
myObj = MyClass( param1, param2, param3 );
or
myObj = MyClass;
myObj.param1 = param1;
myObj.param2 = 7;
etc.
And in terms of running every function in a class, there is really no different to how you put functions together outside of a class, other than that all the data is contained in the class.
You can create a function that will call the other functions in succession if that is what you want. And to print out the final result can be done however you wish. It can be an output argument of this one 'master' function of the class or you can have the class function itself do the actual printing out of it rather than returning it (which seems less useful in general).
Leon Stolp
Leon Stolp on 31 Mar 2020
The properties are not that much of a Problem. I think I'm somewhat overcomplicating things. I attaced my code (It's really messy right now, I'm working on it), whicht is where I am right now. I just cant figure out a way to "run the class" so it calcultes everything.
classdef Holtrop
%UNTITLED2 Summary of this class goes here
% Detailed explanation goes here
properties
g = 9.81;
vis = 1.1392e-06;
rho = 1025.8;
d = -0.9;
end
properties
V
depl
end
properties
Lwl
B
T_F
T_A
T
lcb
A_WL
C_Stern
A_BT
S_APP
APP_factor
h_B
A_T
end
methods %Constructor
function thisHoltrop = Holtrop(Geschwindigkeit, Deplacement)
if nargin == 2
thisHoltrop.V = Geschwindigkeit;
thisHoltrop.depl = Deplacement;
end
end
% messy, needs clean up!
function thisHoltrop = Holtrop1(Lwl, B, T_F, T_A, T, lcb, A_WL, C_Stern, A_BT, S_APP, APP_factor, h_B, A_T)
thisHoltrop.Lwl = Lwl;
thisHoltrop.B = B;
thisHoltrop.T_F = T_F;
thisHoltrop.T_A = T_A;
thisHoltrop.T = T;
thisHoltrop.lcb = lcb;
thisHoltrop.A_WL = A_WL;
thisHoltrop.C_Stern = C_Stern;
thisHoltrop.A_BT = A_BT;
thisHoltrop.S_APP = S_APP;
thisHoltrop.APP_factor = APP_factor;
thisHoltrop.h_B = h_B;
thisHoltrop.A_T = A_T;
end
end
methods
function V = calcV(V)
%Umrechnung von Knoten in m/s
V = V * 0.5144;
end
%--------------------------------------------------------------%
%% Berechnung von Beiwerten und Koeffizienten
function Fn = calcFn(Lwl, V, g)
Fn = V/(sqrt(Lwl*g));
end
function Rn = calcRn(Lwl, V, vis)
Rn = (V*Lwl)/vis;
end
function C_B = calcC_B(Lwl, B, T, depl)
C_B = depl/(Lwl*B*T);
end
function C_WP = calcC_WP(Lwl, B, A_WL)
C_WP = A_WL(Lwl*B);
end
function C_P = calcC_P ( Lwl, depl, A_WL)
C_P = depl/(Lwl * A_WL);
end
function C_M = calcC_M (C_B, C_P)
C_M = C_B/C_P;
end
function C_F = calcC_F(Rn)
C_F = 0.075/((log10(Rn)-2)^2);
end
%--------------------------------------------------------------%
%% Benetzte Fläche S_BH
function S_BH = calcS_BH(Lwl, C_M, C_B, B, T, C_WP, A_BT)
S_BH = Lwl*(2*T+B)*sqrt(C_M)*(0.4530+0.4425*C_B-0.2862*...
C_M-0.003467*(B/T)+0.3696*C_WP)+2.38*(A_BT/C_B);
end
%--------------------------------------------------------------%
%% friction resistance R_F(1+k)
function R_F = calcR_F(rho, C_F, S_BH, V)
R_F = 0.5*rho*C_F*S_BH*V^2;
end
function C_14 = calcC_14(C_Stern)
C_14 = 1+0.011*C_Stern;
end
function L_R = calcL_R(Lwl, C_P, lcb)
L_R = Lwl*(1-C_P+0.06*C_P*lcb/(4*C_P-1));
end
function formfactor = calcformfactor(C_14, B, Lwl, T, L_R, depl, C_P)
formfactor = 0.93+0.487118*C_14*(B/Lwl)^1.06806*(T/Lwl)^0.46106...
*(Lwl/L_R)^0.121563*(Lwl^3/depl)^0.36486*(1-C_P)^(-0.604247);
end
%--------------------------------------------------------------%
%% appendage resistance R_APP
function APP_factor_eq = calcAPP_factor_eq(S_APP, APP_factor)
APP_factor_eq = sum(S_APP*APP_factor)/sum(S_APP);
end
function R_APP = calcR_APP(rho, C_F, S_APP, V, APP_factor_eq)
R_APP = 0.5*rho*C_F*S_APP*V^2*(APP_factor_eq);
end
%--------------------------------------------------------------%
%% wave Resistance R_W
function C_3 = calcC_3(A_BT, B, T, T_F, h_B)
C_3 = (0.56*A_BT^1.5)/(B*T*(0.31*sqrt(A_BT)+T_F-h_B));
end
function C_2 = calcC_2(C_3)
C_2 = exp(-1.89*sqrt(C_3));
end
function C_5 = calcC_5(A_T, B, T, C_M)
C_5 = 1-0.8*A_T/(B*T*C_M);
end
function C_15 = calcC_15(Lwl, depl)
if Lwl^3/depl < 512
C_15 = -1.69385;
elseif 512 <= Lwl^3/depl <= 1726.91
C_15 = -1.69385+(Lwl/depl^(1/3)-8.0)/2.36;
else
C_15 = 0;
end
end
function m_4 = calcm_4(C_15, Fn)
m_4 = C_15*0.4*exp(-0.034*Fn^(-3.29));
end
function lambda = calclambda(C_P, Lwl, B)
if Lwl/B < 12
lambda = 1.446*C_P-0.03*Lwl/B;
else
lambda = 1.446*C_P-0.36;
end
end
function C_16 = calcC_16(C_P)
if C_P<0.8
C_16 = 8.07981*C_P-13.8673*C_P^2+6.984388*C_P^3;
else
C_16 = 1.73014-0.7067*C_P;
end
end
function m_1 = calcm_1(Lwl, T, depl, B, C_16)
m_1 = 0.0140407*Lwl/T-1.75254*depl^(1/3)/Lwl-4.79323*B/Lwl-C_16;
end
function R_W = calcR_W(Fn, B, Lwl, C_WP, lcb, L_R)
if Fn <= 0.4
if (B/Lwl)<0.11
C_7 = 0.229577*(B/Lwl)^0.33333;
elseif 0.11<=(B/Lwl)<0.25
C_7 = (B/Lwl);
else
C_7 = 0.5-0.0625*(Lwl/B);
end
C_7 = round(C_7,5);
i_E = 1+89*exp(-((Lwl/B)^0.80856)*((1-C_WP)^0.30484)*((1-C_P-0.0225*lcb)^0.6367)...
*((L_R/B)^0.34574)*((100*depl/Lwl^3)^0.16302));
i_E = round(i_E,5);
C_1 = 2223105*C_7^3.78613*(T/B)^1.07961*(90-i_E)^(-1.37565);
C_1 = round(C_1,5);
R_W = rho*g*depl*C_1*C_2*C_5*exp(m_1*Fn^d+m_4*cos(lambda*Fn^(-2)));
elseif Fn >= 0.55
C_1 = 6919.3*C_M^(-1.3346)*(depl/Lwl^3)^2.00977*(Lwl/B-2)^1.40692;
C_1 = round(C_1,4);
m_3 = -7.2035*(B/Lwl)^0.326869*(T/B)^0.605375;
m_3 = round(m_3,4);
R_W = rho*g*depl*C_1*C_2*C_5*exp(m_3*Fn^d+m_4*cos(lambda*Fn^(-2)));
else
%R_W(Fn=0.4)+(10Fn-4)*R_W(Fn=0.55)-R_W(Fn=0.55)/1.5
R_W = rho*g*depl*C_1*C_2*C_5*exp(m_1*0.4^d+((C_15*0.4*exp(-0.034*0.4^(-3.29)))*cos(lambda*0.4^(-2))))...
+(10*Fn-4)*...
(rho*g*depl*C_1*C_2*C_5*exp(m_1*0.55^d+((C_15*0.4*exp(-0.034*0.55^(-3.29)))*...
cos(lambda*0.4^(-2))))...
-rho*g*depl*C_1*C_2*C_5*exp(m_1*0.4^d+((C_15*0.4*exp(-0.034*0.4^(-3.29)))*...
cos(lambda*0.4^(-2)))))/1.5; %#ok<NODEF>
end
end
%--------------------------------------------------------------%
%% additional pressure resistance of bulbous bow R_B
function P_B = calcP_B(A_BT, T_F, h_B)
P_B = 0.56*sqrt(A_BT)/(T_F-1.5*h_B);
end
function Fn_i = calcFn_i(V, T_F, h_B, A_BT)
Fn_i = V/sqrt(g*(T_F-h_B-0.25*sqrt(A_BT))+0.15*V^2);
end
function R_B = calcR_B(P_B, Fn_i, A_BT, rho, g)
R_B = 0.11*exp(((-3))*P_B^(-2))*Fn_i^3*A_BT^1.5*rho*g/(1+Fn_i^2);
end
%--------------------------------------------------------------%
%% additional pressure resistance of immersed transom R_TR
function Fn_T = calcFn_T(V, g, A_T, B, C_WP)
Fn_T = V/sqrt(2*g*A_T/(B+B*C_WP));
end
function C_6 = calcC_6(Fn_T)
if Fn_T<5
C_6 = 0.2*(1-0.2*Fn_T);
else
C_6 = 0;
end
end
function R_TR = calcR_TR(rho, C_6, A_T, V)
R_TR = 0.5*rho*C_6*A_T*V^2;
end
%--------------------------------------------------------------%
%% model-ship correlation resistance R_A
function C_4 = calcC_4(T_F, Lwl)
if T_F/Lwl < 0.04
C_4 = T_F/Lwl;
else
C_4 = 0.04;
end
end
function C_A = calcC_A(Lwl, C_B, C_4)
C_A = 0.006*(Lwl+100)^(-0.16)-0.00205+0.003*sqrt(Lwl/7.5)*C_B^4*C_2*(0.04-C_4);
end
function R_A = calcR_A(rho, C_A, S_BH, V)
R_A = 0.5*rho*C_A*S_BH*V^2;
end
%--------------------------------------------------------------%
%--------------------------------------------------------------%
%% total resistance R_T
function R_T = calcR_T(R_F, formfactor, R_APP, R_W, R_B, R_TR, R_A)
R_T = R_F*formfactor+R_APP+R_W+R_B+R_TR+R_A;
end
end
end

Sign in to comment.

Answers (2)

Peng Li
Peng Li on 31 Mar 2020
You have too many class methods that are not really methods specific for the class, as they never accept your object handle as an input. If they are necessary helper functions, make then out of the class def block. If they are necessarily need to be class methods, you'd better make the handle as the input, and within the function block, call explicitely the properties.
If you find it too difficult to organize so many properties (at least I do), you could probably categorize them into different groups by using struct. Your property names are too difficult to remember anyway.
  3 Comments
Peng Li
Peng Li on 31 Mar 2020
this looks strange as get.V supposes to get the value of V. It's weired that you make it a different thing while trying to access it. If this is a necessary calculation, it makes more sense for me to simplify it by Vnew = obj.v * .5144 whenever you want to use it. This makes the class body cleaner so that you don't necessarily need to write up a get.V function for it.
Leon Stolp
Leon Stolp on 31 Mar 2020
Okay, I understand that. Do you have any advice on the Error? It's not only a Problem with this particular function, so I need to fix it.

Sign in to comment.


Steven Lord
Steven Lord on 31 Mar 2020
Edited: Steven Lord on 31 Mar 2020
To me, it seems like most if not all of these "calc<something>" methods should probably be property get methods and the properties those methods calculate should be Dependent properties. See the "Calculate Data on Demand" link in the description of the Dependent attribute on that page for more information.
But this assumes that you actually need to use a class. You don't really seem to have any methods that do something other than calculating a value. To me that suggests that a plain old function (perhaps one that operates on a struct array, to avoid requiring passing in fifteen input arguments in a specific order) would be better suited.
function HC = holstropCharacteristics(Geschwindigkeit, Deplacement, HolstropProperties)
% Define constants
g = 9.81;
vis = 1.1392e-06;
% % Unpack the properties if you want
Lwl = HolstropProperties.Lwl;
V = = HolstropProperties.V;
% and calculate with the unpacked properties
HC.Fn = calcFn(Lwl, V, g);
% Or pass the properties into the subfunctions en masse
% and let the subfunction retrieve what they need or want
HC.Rn = calcRn(HolstropProperties, vis);
% etc.
end
function Fn = calcFn(Lwl, V, g)
Fn = V/(sqrt(Lwl*g));
end
function Rn = calcRn(HP, vis)
Rn = (HP.V*HP.Lwl)/vis;
end
  5 Comments
Leon Stolp
Leon Stolp on 1 Apr 2020
Alright, you're really helping me out! Thanks a lot!
I'm currently trying to assign my Properties as structs, with actual progress so far. I will see if my progress is keeping on.
My Professor is kind of demanding that I work with a class, I can't figure it out myself really. I'm going to ask him tomorrow.
Leon Stolp
Leon Stolp on 1 Apr 2020
So, here I am again... Kinda feeling stupid right now.
I managed to struct all my properties, and I'm able to assign values to them. Now my problem is, that i can't manage to call the get Method for those dependent struct properties.
properties (Constant, Access = private)
Constant = struct('g', 9.81,...
'vis', 1.1392e-06,...
'rho', 1025.8,...
'd', -0.9);
end
%Vorgegebene Werte
properties
set = struct( 'V',[], 'depl',[]);
%Zu optimierende Werte
opt = struct ('Lwl',[],...
'B',[],...
'T_F',[],...
'T_A',[],...
'T',[],...
'lcb',[],...
'A_WL',[],...
'C_Stern',[],...
'A_BT',[],...
'S_APP',[],...
'APP_factor',[],...
'h_B',[],...
'A_T',[])
end
%Errechnete Werte
properties (Dependent)
Numbers = struct (Fn, Rn)
Coeff = struct (C_B, C_WP, C_P, C_M, C_F)
S_BH
R_F
ff = struct (C_14, L_R, formfactor)
Rapp = struct (APP_factor_eq, R_APP)
Rw = struct (C_3, C_2, C_5, C_15, m_4, lambda, C_16, m_1, R_W) %#ok<*MDEPIN>
Rb = struct (P_B, Fn_i, R_B)
Rtr = struct (Fn_T, C_6, R_TR)
Ra = struct (C_4, C_A, R_A)
R_T
end
%---------------------------------------------------------------------%
%Methods
%---------------------------------------------------------------------%
methods
%Constructor
function H84 = Holtrop84(Lwl, B, T_F, T_A, T, lcb, A_WL, C_Stern,...
A_BT, S_APP, APP_factor, h_B, A_T)
if nargin == 13
H84.opt.Lwl = Lwl;
H84.opt.B = B;
H84.opt.T_F = T_F;
H84.opt.T_A = T_A;
H84.opt.T = T;
H84.opt.lcb = lcb;
H84.opt.A_WL = A_WL;
H84.opt.C_Stern = C_Stern;
H84.opt.A_BT = A_BT;
H84.opt.S_APP = S_APP;
H84.opt.APP_factor = APP_factor;
H84.opt.h_B = h_B;
H84.opt.A_T = A_T;
end
end
function a= get.Numbers.Fn(obj)
a = obj.V/(sqrt(obj.g*obj.Lwl));
end
At the end I'm trying to assign the function to the dependent propertie Fn within the Numbers struct. I tried some different ways but always end up with an Error like
Cannot specify a get function for property 'Fn' in class 'Holtrop84', because that property
is not defined by that class.
Did I mess up the command, or are my structs messed up?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!