Writing a Loop function for all variable possibilities in two arrays.

조회 수: 3(최근 30일)
I have writtent he below array to generate two random numbers r1 & r2, and then convert them into metres and then use these numbers to calculate relevant dimensions on a beam for calculating the resultant deflection of a beam with a partially distributed load. The numbers first correspond to the length of that partially distributed load (PDL) (r1), and the distance that load is from the tip of the beam. This latter number is then used to calucate the two respective heights of the PDL in order to calculate UDLS for them.
I am using the following function for this:
and then
This current script does this for one generated number and one generated gel distance. However I would like it to do this for all possible combinations (of which there are 12 numbers for r1, and 11 different variations of that for r2.
How would I define the loop function here to be able to do this, and then calculate the deflections for each of these combinations and then plot the
plot with this data.
Here is the script I have written for this.
l = 0.012 % Height in mm of Beam // MILLIMETRES
r = 0.0005 % radius of beam in mm // MILLIMETRES
E = 2.26e6 % Youngs Modulus of PDMS // PASCAL
r1 = randperm(12,1) % choose a random size of the gels width
r2 = randi([0,12]) % choose the distance that gel is from the tip of the post
r3 = r2/1000 % distance from tip converted in to metres
r4 = l - r3 %This is Height 1, total beam length minus the distance the gel is from the tip - height 1 from base
r5 = r1/1000 % this is the gel width in metres, this is the length fo the partial load
H1 = l - r3 % how to calculate Height 1
H2 = l - (r3 + r5) % how to calculate H2
H3 = (r3+r5) %This is the tip remaining for H2,
%Calculating a Partial UDL of a Hydrogel attached to a post accross a
%length from the tip to a known distance down the beam (h2).
%Created 24/11 RMTS
%set beam properties
%Define the load
%The partial load will be applied over a distance of 4mm, from 8mm on the
%beam until the tip at 12mm
h1 = H1 %top of this partial load
h2 = H2 % bottom of this partial load
d1 = r5 %length of the partial load
%calculate the UDL of the whole beam. This will have a distribtued force w
%over the total length of the beam.
P = 0.0001:0.00001:0.1; %force in NEWTONS
w = P % force over distance N/m
I = pi*r^4/4; %second moment of innertia
UDL = w*(l^4)/(8*E*I) %This is the UDL over the whole length of the beam
%Now calculate the UDL over the smaller length up to h2. Need to define P,
%w and I again
P = 0.0001:0.00001:0.1; %Same force as above in NEWTONS
w = P %This UDL is only up to H2, so the length is h2
I = pi*(r^4/4); %second moment of innertia
UDL_h2 = w*(h2^4)/(8*E*I) %Calculates the UDL over length h2, and w; w2
%Now calculate that tip slope deflection. This the angle multiplied by the
%length difference
%Define the length difference l-a
d1 = H3
%TDL is tip deflection, Angle refers to theta as defined in my notes
Angle = w2 * (h2^3) / (6 * E * I)
TDL = Angle * d1
%now subtract TDL from UDL
Total = UDL - TDL
hold on
title('Plot of Deflection')
xlabel('Force in Newtons')
ylabel('Deflection in Metres')
Any help would be greatly appreciated.
Thanks in advance.
  댓글 수: 1
dpb 2020년 11월 25일
"would I define the loop function here to be able to do this..."
The MATLAB way would be to eschew the loops. Look at
doc meshgrid
Also NB: the note hidden therein --
"Starting in R2016b, it is not always necessary to create the grid before operating over it. For example, computing the expression x*exp(−x^2−y^2) implicitly expands the vectors x and y. For more information on implicit expansion, see Array vs. Matrix Operations."

댓글을 달려면 로그인하십시오.


Shraddha Jain
Shraddha Jain 2020년 12월 15일
Hi Robert,
The best way to get all possible combinations of two vectors in matrix form is by using meshgrid function. In your case, it would be implemented as,
[R1,R2] = meshgrid(r1,r2);
You may refer to the documentation of meshgrid for more details.


Find more on Creating and Concatenating Matrices 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!

Translated by