Applying multiple constraints for while loop saving final output variables together

5 views (last 30 days)
I have a code that runs a function within a while loop until the output of that variable reaches a size of [i,m] where i is my constraint. I need to run the while loop for 3 different constraint variables and need to obtain 3 different output variables. My solution was to copy paste the code 3 times and edit the while loop so it outputs a different variable for each constraint. here is the code:
p2=0;
np=2;
while i<np
p2=p2+.0001;
pose=planarfkp(5,0,6,3*sqrt(2),3,pi/4,5,p2,3,0);
[i,m]=size(pose);
p2low2=p2;
end
p2=0;
np=4;
while i<np
p2=p2+.0001;
pose=planarfkp(5,0,6,3*sqrt(2),3,pi/4,5,p2,3,0);
[i,m]=size(pose);
p2low4=p2;
end
p2=0;
np=6;
while i<np
p2=p2+.0001;
pose=planarfkp(5,0,6,3*sqrt(2),3,pi/4,5,p2,3,0);
[i,m]=size(pose);
p2low6=p2;
end
I did try to create a vector that contained the values for mp and then put the while loop inside a for loop that indexed the values from that vector and it worked and returned one value I needed but it didnt save all of the output variables i need for each variable constraint. I wrote that like this:
clear all
p2=0;
npv=[2 4 6];
for j = 1:3
np=npv(j);
while i<np
p2=p2+.0001;
pose=planarfkp(5,0,6,3*sqrt(2),3,pi/4,5,p2,3,0);
[i,m]=size(pose);
p2out=p2;
end
end
The end product I am trying to find is a 2x3 matrix that looks like
Also, if didn't you notice the whole goal is to find for what values of p2 the size(pose) changes. I am trying to find the intervals for the values of p2 that change the size(pose). The planarfkp function is for a 2d stewart platform kinematic forward problem. All of the other constraints remain constant while p2 varies and the function outputs the configurations of the stewart platform. There is an interval of p2 where there are 0 configurations, another that has 2, and then 4, and then 6. My elementary solution was just to step p2 up from 0 by 0.0001 but I am sure there is a better way to write this as well.

Accepted Answer

Jan
Jan on 21 Feb 2021
Your original code does not run, because the variable "i" is not defined initially. Maybe Matlab uses the default definition of "i" as imaginary unit and the code runs by accident. I guess you want:
p2 = 0;
np = 2;
h = 0;
while h < np
p2 = p2 + 0.0001; % So the code is not evaluated at p2=0?
pose = planarfkp(5,0,6,3*sqrt(2),3,pi/4,5,p2,3,0);
h = size(pose, 1);
end
p2low2 = p2; % Outside the loop.
And to do this in a loop:
npv = [2 4 6];
result = zeros(numel(npv), 2);
for k = 1:3
p2 = 0; % Inside the loop
np = npv(k);
while h < np
p2 = p2 + 0.0001;
pose = planarfkp(5,0,6,3*sqrt(2),3,pi/4,5,p2,3,0);
h = size(pose, 1);
end
result(k, 1) = np;
result(k, 2) = p2;
end
  6 Comments
Jan
Jan on 22 Feb 2021
There is no difference between input and output variables. The indexing works in the sme way on the left and right side of the = operator.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!