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

1 ビュー (過去 30 日間)
Mohammed Lamine Mekhalfia 2022 年 6 月 3 日
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)
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)
k=TRot/100 %timestep
h=1
f=1
for N=1:1: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 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)
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)
k = TRot/100; %timestep
% Define vector variables [initial value:step:final value]
N = 1:1:Holes;
time = 0:k:TRot;
% Computations
Pressure_Out = Pressure_In2 * time;
% Plot
plot(time, Pressure_Out)
xlabel('Time [s]', 'fontsize', 16, 'Interpreter', 'latex');
ylabel('Pressure [bar]', 'fontsize', 16, 'Interpreter', 'latex'); ##### 0 件のコメント表示非表示 -1 件の古いコメント

サインインしてコメントする。

Mohammed Lamine Mekhalfia 2022 年 6 月 3 日
in fact below a better explanation of what I am seeking サインインしてコメントする。

### カテゴリ

Find more on Get Started with MATLAB in Help Center and File Exchange

R2020a

### Community Treasure Hunt

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

Start Hunting!