How to use methods of the class in the constructor of this class?

10 views (last 30 days)
Dear friends!
I am quite new with Matlab and especially with OOP in it. I have a question regarding object construction:
the task:
to get basic parameters of an object - a journal fluid-film bearing - given initial parameters: geometric, operational and so on.
why am I using OOP: i want to ultimately get a program that could run on any given number of bearings as usually there are at least two of them in one system. creating new instances of the class 'JournalBearing' would allow vibrational analysis of a multi-bearing system.
the question:
i want to create an object of the class in question given the initial parameters:
jb1 = JournalBearing(L,R,h0,m,n); <- here L and R are length and radius, h0 - radial clearence (difference between bearing radius and rotor radius, usually a very small value in microns), m and n are mesh parameters: determination of bearing characterisitcs requires solution of a PDE over some m x n mesh.
I guess there should be more parameters in the constructor here to get properties of the object.
I have developed a few methods that help define some of the parameters of the bearing: radial gap function, pressure distribution in the fluid film and some mathematical methods - derivatives along two coordinates. So far I have been able to determine these parameters by means of simply calling the methods:
[h,dhdx] = jb1.RadGap(X0,Y0); <- method returns the radial gap function and its derivative given initial rotor coordinates;
[p]=jb1.PresDis(h,dhdx,n_rpm,mu,Xdot,Ydot,PresCond); <- method returns pressure distribution - an m x n matrix given some operational parameters, initial rotor velocity and pressure conditions around the bearing - we set it to 0 as its ambient pressure all around, although could be different with axial fluid supply.
Now there is my problem: I want only one line instead of these three: I want the line
jb1 = JournalBearing(L,R,h0,m,n, ...../* all other parameters */);
to result in creation of an object jb1 with all the parameters such as radial gap, its derivative, pressure distribution and others as its properties. So, as I would put it, I need the constructor to call the developed methods and assign the output value to the corresponding property.
here is what the class looks like now with the present constructor:
classdef JournalBearing < handle
properties
Length
RadiusJournal
RadiusBearing
RadClearence
MeshX
MeshZ
RadialGap
PressureDistribution
LoadCapacity
end
methods
function thisTarget = JournalBearing(l,R,h0,m,n)
thisTarget.Length=l;
thisTarget.RadiusJournal=R;
thisTarget.RadiusBearing=R+h0;
thisTarget.RadClearence=h0;
thisTarget.MeshX=linspace(0,2*pi,m);
thisTarget.MeshZ=linspace(0,thisTarget.Length,n);
end
and here is the radial gap function method to assign the output to the property:
function [h,dh] = RadGap(obj,X,Y)
m=length(obj.MeshX);
n=length(obj.MeshZ);
h=zeros(m,n);
dx=obj.MeshX(2)-obj.MeshX(1);
dz=obj.MeshZ(2)-obj.MeshZ(1);
for i=1:m
for j=1:n
h(i,j)=obj.RadClearence-X*sin(obj.MeshX(i))-Y*cos(obj.MeshX(i));
end
end
dh=obj.XDerivative(h,dx);
obj.RadialGap=h;
end
The main question being: is there any reason to divide the code into small methods in order to fill in the properties like that without calling the methods from the main file? I think, theoretically, I could just have one method - constructor - doing all the work and assign results to properties as I get them which would result in merging all methods into one and having tens of input parameters, or could I call methods from the constructor somehow?
I hope it is understandable. Thank you very much for any help, if you have any questions, feel free to ask.

Accepted Answer

Guillaume
Guillaume on 14 Jan 2020
Having a method that calculates a particular property: No problem, particularly if the calculation is fairly complex as you have here.
Your current design is a bit iffy though. You need to decide whether that method sets the property RadialGap itself, in which case that property shouldn't return anything (and should be a private or local method), or the method returns the radial gap and derivative (because the user of the class may want to call it), in which case it doesn't set the property (and is public).
So you'd either have:
classdef JournalBearing < handle
%property definitions
methods
function this = JournalBearing(length, radiusjournal, radiusbearing, obscureparameter, anotherobscureparameter, initialrotorcoordinates)
%property initialisation as usual
InitialiseRadialGap(this, initialrotorcoordinates);
end
end
methods (Access = private)
function InitialiseRadialGap(this, initialrotorcoordinates)
%calculations note that the code can be simplified to
this.RadialGap = repmat(this.Clearance - X*sin(this.MeshX(:)) - Y*cos(this.MeshX(:)), 1, numel(this.MeshZ)); %loop not needed at all! Not sure why you were calculated dz which is not used
end
end
end
or
classdef JournalBearing < handle
%property definitions
methods
function this = JournalBearing(length, radiusjournal, radiusbearing, obscureparameter, anotherobscureparameter, initialrotorcoordinates)
%property initialisation as usual
this.RadialGap = CalculateRadialGap(this, initialrotorcoordinates)
end
function [radialgap, radialgapderivative] = CalculateRadialGap(this, initialrotorcoordinates)
%calculations note that the code can be simplified to
radialgap = repmat(this.Clearance - X*sin(this.MeshX(:)) - Y*cos(this.MeshX(:)), 1, numel(this.MeshZ)); %loop not needed at all! Not sure why you were calculated dz which is not used
radialgapderivative = gradient(radialgap(:, 1), this.MeshX); %I assume that's what your derivative function does
end
end
end
For self-documenting purpose, I would recommend that you use full words for the function inputs and outputs as well as the function names.
  1 Comment
Alexander Babin
Alexander Babin on 14 Jan 2020
Dear Guillaume, thank you very much for your answer! It worked perfectly, additional thanks for the repmat function! :)

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!