Help with loop and tracking values.

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 Kommentare

Image Analyst
Image Analyst am 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
John Biscanti am 19 Sep. 2020
Bearbeitet: Image Analyst am 19 Sep. 2020
My apolgies for the confusion. Here is the full code. I am only concerned with the second for loop for my problem. The one with ii:n2 and jj as the length of zeta. Thank you
clc
clear
T0=1;
tf = .01;
h= 0.001;
y0 = [0, 0];
n = int32(ceil(tf/h))+1; % determine the number of steps
t = linspace(0, tf, n); % Generate a time step vector
y = zeros(n,2); % Allocate the array for numerical solutions
y(1,:) = y0'; % The first row of y is the initial condition
A = zeros(n,21); % Allocate the array of acceleration values
%----------------------------------------------------------------------
% TODO: Implement the loop
%----------------------------------------------------------------------
zeta = 0:0.01:0.05;
for ii = 1:(n)
for jj = 1:length(zeta)
k1 = func2(t,zeta(jj),y(ii,:))';
k2 = func2(t+h/2,zeta(jj),(y(ii,:)+ h/2*k1))';
y(ii+1,:) = y(ii,:) + h*k2;
A(ii,jj) = -10000*y(ii,1)-200*zeta(jj)*y(ii,2)+1;
end
end
%Amax = max(A,[],'all') %Amax
%A(11,6) %sanity check
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;
if (B <= 0.05 & B(ii) >= .05)
end
end
end
Image Analyst
Image Analyst am 19 Sep. 2020
Bearbeitet: Image Analyst am 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
John Biscanti am 19 Sep. 2020
I am not sure, that was me attempting to do what I needed, but I am not super skilled with MATLAB and is why I was asking.
John Biscanti
John Biscanti am 19 Sep. 2020
I think I just thought it would be comparing acceleration values and the t values that B is using

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Mohith Kulkarni
Mohith Kulkarni am 24 Sep. 2020

0 Stimmen

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

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Tags

Gefragt:

am 19 Sep. 2020

Beantwortet:

am 24 Sep. 2020

Community Treasure Hunt

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

Start Hunting!

Translated by