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

3 次查看(过去 30 天)
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
h=1
f=1
for N=1:1:Holes
s=(alpha_rad/omega_rad)+N*(pi/Holes)
for time=0:k:TRot
Pressure_Out=Pressure_In1*time
Press(h)=Pressure_Out
t(h)=time
h=h+1
if time==s
Pressure_Out=Pressure_In2*time
end
end
S(f)=s
f=f+1
end
plot(t,Press)

回答(2 个)

Alberto Cuadra Lara
编辑:Alberto Cuadra Lara 2022-6-3
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

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by