Help with loop and tracking values.
Show older comments
Hello,
I have an array full of acceleration values called B. B is calculated using the function at the bottom of the page. The rows of B correspond to the time and the columns correspond to a variable called zeta. Acceleration is calculated using these values of zeta and t that increment each iteration.
I need to figure out how to find the values of zeta which make the acceleration values in B less than or equal to 0.05 when t is greater than or equal to 0.05
How would I do this? Note: each row the time increases by 0.001 seconds, and each column of zeta increases by 0.1. Thank you.
Code:
T02=1;
tf2 = .2;
h2= 0.001;
y02 = [0, 0];
n2 = int32(ceil(tf2/h2))+1; % determine the number of steps
t2 = linspace(0, tf2, n2); % Generate a time step vector
y2 = zeros(n2,2); % Allocate the array for numerical solutions
y2(1,:) = y0'; % The first row of y is the initial condition
B = zeros(n2,21); % Allocate the array of acceleration values
zeta2 = 0:0.1:2;
for ii = 1:(n2)
for jj = 1:length(zeta2)
k1 = func2(t2,zeta2(jj),y(ii,:))';
k2 = func2(t2+h2/2,zeta2(jj),(y(ii,:)+ h2/2*k1))';
y(ii+1,:) = y(ii,:) + h2*k2;
B(ii,jj) = -10000*y(ii,1)-200*zeta2(jj)*y(ii,2)+1;
end
end
Here is func 2 if needed:
function dy = func2(t,zeta,y)
dy = [y(:,2);-10000*y(:,1)-200*zeta*y(:,2)+1];
end
5 Comments
Image Analyst
on 19 Sep 2020
We can't run your code:
>> test3
Unrecognized function or variable 'y0'.
Error in test3 (line 8)
y2(1,:) = y0'; % The first row of y is the initial condition
So, what is y0?
John Biscanti
on 19 Sep 2020
Edited: Image Analyst
on 19 Sep 2020
Image Analyst
on 19 Sep 2020
Edited: Image Analyst
on 19 Sep 2020
B is a 2-D matrix, so exactly what are your thoughts here when you reference it with no, and one, index:
B(ii,jj) = -10000*y(ii,1)-200*zeta2(jj)*y(ii,2)+1;
if (B <= 0.05 & B(ii) >= .05)
John Biscanti
on 19 Sep 2020
John Biscanti
on 19 Sep 2020
Answers (1)
Mohith Kulkarni
on 24 Sep 2020
Hi, you are trying to find the values of "zeta2" for which the values in "B" are <= 0.05 for the last 151 rows(concluded this from "t">=0.05). Refer to the below code:
t05 = find(t2==0.05); %t05 = 51, the index corresponding to time step 0.05
temp = B(t05:end,:)<=0.05; %logical matrix for B values <= 0.05 and t >= 0.05
you can retrieve zeta values for any timestep by indexing into the "zeta2". refer to below code for example:
tsq = 0.06 %time step 0.06
zeta_vals = zeta2(temp(find(t2 == tsq)-50,:))
this code should be after the loop as we are accessing the "zeta" values after computing "B" matrix values
Categories
Find more on Loops and Conditional Statements 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!