Function handle not rechecking if statement
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I am trying to model a buoy in sinesoidal waves. I am using function handles to do this. There is 4 if conditions:
- When there is no itersection between the buoy and the wave and the buoy is obove water
- When there is no itersection between the buoy and the wave and the buoy is below water
- When there is itersection between the buoy and the wave less that 50%
- When there is no itersection between the buoy and the wave greater than 50%
However what occurs is that it runs through the inital values and determines which if statement to use in the for loop and then just uses that value, even though the values effecting the if statement above the for loop are changing after each loop.
tf_f = @(T,Y) isempty(p_f(T,Y)); % Is the matrix empty == true(1) or false (0) [ is there intersection points from the buoy and wave surface]
%% Conditions for forces
% float
if tf_f(T,Y) == 1 && bottom_f(Y) > p21top_f(T) %if intersection matrix is empty and bottom of buoy is above the wave surface
elseif tf_f(T,Y) == 1 && top_f(Y) < p21top_f(T) %if intersection matrix is empty and top of buoy is below the wave surface
elseif tf_f(T,Y) == 0 && centre_check_f(Y) > p21top_f(T) %if intersection matrix is full and centre of buoy is above the wave surface
elseif tf_f(T,Y) == 0 && centre_check_f(Y) < p21top_f(T) %if intersection matrix is full and centre of buoy is below the wave surface
end
Above is the if statement that decides the how it should act, tf_f is used to determine whether the intersection matrix is empty, however it only does the above for the inital and does not update? after this the Runga_kutta 4th order intergration takesplace in a for loop
%Float (centre)
Accel_h_f = @(T,Y,V) ((Sum_forces_h_f(T,Y,V))/(abs(mass_f)));
for i=1:(length(t)-1) % calculation loop
k_1vh_f = Accel_h_f(t(i), y_f(i), v_f(i))*h;
From what i can tell, all other parts of my code are correct and for each of the conditions above. I just cant seem to figure out how to get the code to check the if statement each time. Any suggestions?
0 Kommentare
Antworten (1)
dpb
am 23 Apr. 2019
Bearbeitet: dpb
am 24 Apr. 2019
tf_f = @(T,Y) isempty(p_f(T,Y));
if tf_f(T,Y) == 1 && bottom_f(Y) > p21top_f(T)
elseif tf_f(T,Y) == 1 && top_f(Y) < p21top_f(T)
elseif tf_f(T,Y) == 0 && centre_check_f(Y) > p21top_f(T)
elseif tf_f(T,Y) == 0 && centre_check_f(Y) < p21top_f(T)
end
Not enough code to be able to tell...we don't know where the loop is which is crucial...
But, the crystal ball is back from the shop so let's give it a whirl and see if it is (finally!) repaired or not--
It says, since the content of any variable not passed as a dummy argument into an anonymous function is the value for that variable in the workspace at the time the anonymous function is defined and is invariant, that definition is outside the loop and so the array p_f is constant in the function definition instead of being revised as you expect.
Either pass p_f, too, or don't use the anonymous function but just write the test expression.
(Now to wait and see if I wasted money on the repair bill... :) )
2 Kommentare
dpb
am 28 Apr. 2019
Well, with all the code you did show, you didn't show us anything related to the error itself...namely the function that had the error.
However since the crystal ball does seem to have been repaired, and it even has a little bit of information to work with this time; let's put it to the test again-- :)
Index in position 2 exceeds array bounds.
Error in Floating_perfect_Save_Spare>@(Q,Z,S)Q(Z,S)
Error in Floating_perfect_Save_Spare>@(T,Y)select(p(T,Y),1,2)
Since the error message says you've exceeded an array bound and it's specifically the second argument to the function which is Z, one would presume the most likely cause is you have a loop over the size of the array that is looking at positions Z(i),Z(i+1) and when i reaches the size of the array Z, then Z(i+1) is out of bounds.
Very common logic error to make; simply make the upper loop bound one less than the array size in the particular dimension.
Of course, you need to also think about your intersection search logic -- if it is at some boundary, can you actually reach the intersection itself by the search you are using? Work out with pencil and paper what you're actually looking at and see for the end conditions...
Siehe auch
Kategorien
Mehr zu Graphics Object Programming finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!