How to compute values using for loop then depending on time interval we plot the output?

1 view (last 30 days)
Dear Matlab comunity.
I hope this post finds you well.
My problem is as the following
I have two input values (0,1), where the output is strictly attached to them.
for a t=0 to Tfin, the output would be 0 unless for some designated time interval calculated.
I hope I explained well, I am not good in that and my English is a bit at a biginner level.
The code is as follow ( it is not finished yest)
clear all
omega=1000 %rotation frequency (rpm)
omega_rad=(omega*2*pi)/60 %rotation frequency (rad/s)
TRot=(360*(pi/180))/omega_rad %Time of one rotation
Holes=15 %Number of holes in Cylinder
Pressure_In1=0 %Pressure Amplitude in Bar
Pressure_In2=1 %Pressure Amplitude in Bar
alpha=360/Holes %angles between holes (degree)
beta=11.20 %angles swept by 1 hole (degree)
alpha_rad=alpha*(pi/180) %angles between holes (rad)
beta_rad=beta*(pi/180) %angles swept by 1 hole (rad)
k=TRot/100 %timestep
for N=1:1:Holes
for time=0:k:TRot
if time==s

Answers (2)

Alberto Cuadra Lara
Alberto Cuadra Lara on 3 Jun 2022
Edited: Alberto Cuadra Lara on 3 Jun 2022
Hello Mohammed,
There is no need to use loops here. MATLAB works great with vectors and is designed for this purpose. I hope this example helps you. I have changed the definition of Pressure_Out, because Pressure_In1 is zero.
If you multiply vectors element by element you have to proceed like A .* B, don't forget the period.
% Constants
omega = 1000; %rotation frequency (rpm)
omega_rad = (omega*2*pi)/60; %rotation frequency (rad/s)
TRot = (360*(pi/180))/omega_rad; %Time of one rotation
Holes = 15; %Number of holes in Cylinder
Pressure_In1 = 0; %Pressure Amplitude in Bar
Pressure_In2 = 1; %Pressure Amplitude in Bar
alpha = 360/Holes; %angles between holes (degree)
beta = 11.20; %angles swept by 1 hole (degree)
alpha_rad = alpha*(pi/180); %angles between holes (rad)
beta_rad = beta*(pi/180); %angles swept by 1 hole (rad)
k = TRot/100; %timestep
% Define vector variables [initial value:step:final value]
N = 1:1:Holes;
time = 0:k:TRot;
% Computations
s = (alpha_rad / omega_rad) + N * (pi/Holes);
Pressure_Out = Pressure_In2 * time;
% Plot
plot(time, Pressure_Out)
xlabel('Time [s]', 'fontsize', 16, 'Interpreter', 'latex');
ylabel('Pressure [bar]', 'fontsize', 16, 'Interpreter', 'latex');

Mohammed Lamine Mekhalfia
@Alberto Cuadra Lara Thanks a lot for your reply
in fact below a better explanation of what I am seeking


Find more on Get Started with MATLAB 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!