0% found this document useful (0 votes)
12 views

Lecture4 Algorythms

Here are the key steps to solve this problem: 1. Calculate the speed after distance d1 using the kinematic equations: v = v0 + a*t s = s0 + v0*t + 0.5*a*t^2 2. Calculate the time to reach d1. Plug t back into the speed equation to get v1. 3. From d1 to d2, the speed is constant at v1. Calculate the time to reach d2 using: s = s0 + v*t 4. Output the position, speed, time at regular intervals using a loop with time step dt. The position and speed will change according to
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Lecture4 Algorythms

Here are the key steps to solve this problem: 1. Calculate the speed after distance d1 using the kinematic equations: v = v0 + a*t s = s0 + v0*t + 0.5*a*t^2 2. Calculate the time to reach d1. Plug t back into the speed equation to get v1. 3. From d1 to d2, the speed is constant at v1. Calculate the time to reach d2 using: s = s0 + v*t 4. Output the position, speed, time at regular intervals using a loop with time step dt. The position and speed will change according to
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

Algorithms

TASK 14
Draw a set of n integers from the interval <-30, 30>

Operator : % remainder after dividing one integer by another


10%8 2 15%5 0 22%7 1

rand() function that generates an integer random number from the range < 0 - RAND_MAX >
RAND_MAX = 36767
rand()%11 random integers: 0 1 2 3 4 5 6 7 8 9 10
rand()%11 -10 random integers: -10 -9 -8 … 0
rand()%21 -10 random integers: -10 -9 -8 … 0 +1 … +10

random integers from the range <p1,p2>


rand()%(p2-p1+1)+p1 rand()%(30-(-30)+1)+(-30) rand()%61-30 < -30, 30 >
START

Declaration integer i, k, n , p1, p2

Input n, p1, p2

i=1
FALSE
i<=n
TRUE
k = rand()%(p2-p1+1)+p1
loop

Show k

i = i +1

STOP
#include <iostream>
#include <cstdlib> // function rand()
using namespace std;
int main() { // ------------------------------
int k,n,p1,p2;
cout<<" number of draws n="; cin>>n;
cout<< "the smallest integer drawn number ="; cin>>p1;
cout<< "the highest integer drawn number ="; cin>>p2;

for (int i=0; i<=n; i++){//***


k=rand()%(p2-p1+1)+p1;
cout << "draw "<< k << endl;
}// *****************

return 0;
}// ---------------------------
TASK 15
Draw a set of n real numbers from the interval <-50, 50>

rand() function that generates an integer from the range < 0 - RAND_MAX >
RAND_MAX = 36767

double integer

1.*rand()/RAND_MAX real random number from the range < 0, 1 >

real random number from the range < p1, p2 >


1.*rand()/RAND_MAX *(p2-p1)+p1
1.*rand()/RAND_MAX (50-(-50)+(-50)
1.*rand()/RAND_MAX *100 - 50 < -50, 50 >
START

Declaration integer i, n

Declaration real k, p1, p2

Input n, p1, p2

i=1
FALSE
i<=n
TRUE
k = 1.*rand()/RAND_MAX *(p2-p1)+p1
loop

Show k

i = i +1

STOP
#include <iostream>
#include <cstdlib> // function rand()
using namespace std;
int main() { // ------------------------------
int n;
double k,p1,p2;
cout<<" number of draws n="; cin>>n;
cout<< " the smallest real drawn number ="; cin>>p1;
cout<< " the highest real drawn number ="; cin>>p2;

for (int i=0; i<=n; i++){// *******


k= 1.*rand()/RAND_MAX*(p2-p1)+p1;
cout << " draw "<< k << endl;
} // *****************
return 0;
}// ------------------------------
TASK 16
Draw a set of n=100 real numbers from the interval <-150, 150>
Show on the screen the values from the range<-10, 17> (data filtering)

real random number from the range < p1, p2 >


1.*rand()/RAND_MAX *(p2-p1)+p1
1.*rand()/RAND_MAX *300 - 150 < -150, 150 >

data filtering from range < d1 , d2>


START
Declaration integer i, n
Declaration real k, p1, p2 , d1, d2
Input n, p1, p2 , d1, d2
i=1
FALSE
i<=n
TRUE
k = 1.*rand()/RAND_MAX *(p2-p1)+p1
loop

TRUE
d1<=k<=d2 Show k
FALSE
i = i +1

STOP
int n;
double k,p1,p2, d1,d2;
cout<<" number of draws n="; cin>>n;
cout<< " the smallest real drawn number ="; cin>>p1;
cout<< " the highest real drawn number ="; cin>>p2;
cout<< " the smallest shown number ="; cin>>d1;
cout<< " the highest shown number ="; cin>>d2;

for (int i=0; i<=n; i++){ // *****************


k= 1.*rand()/RAND_MAX*(p2-p1)+p1;
if (k>=d1 && k<=d2) cout << " draw "<< k << endl;
} // *****************
return 0;
}// ------------------------------
TASK 17
Generating numbers of an arithmetic series
x up to the specified maximum value and
computing the value of the function y= f(x)
for these values according to the formula:
𝟓
𝒚 = 𝐬𝐢𝐧(𝒙/𝟑) + √𝒙 + 𝐥𝐨𝐠 𝟏𝟎 𝒙 + √𝒙

Show values of y for y > y1.


(data filtering)
Maths model
value x value xmax arithmetic series with difference 1.5
x=1.5 3.0 4.5 … xmax value y = f(x)
𝟓
𝒇(𝒙) = 𝐬𝐢𝐧(𝒙 /𝟑) + √𝒙 + 𝐥𝐨𝐠 𝟏𝟎 𝒙 + √𝒙
value y1=8
START

Declaration x, xmax=50, y, y1
Input y1

x = 1.5
FALSE
x<=xmax
TRUE
y = f(x)
TRUE
loop

y>y1 Show x, y

FALSE
x = x +1.5

STOP
#include <iostream>
#include <cmath>
using namespace std;
int main(){
double x(1.5), xmax=50, y, y1; // declaration
cout<< "y1 "; cin>>y1;

while (x<=xmax) { // start of the loop


y=sin(x/3)+sqrt(x) + log10(x)+ pow(x,0.2);
if (y>y1) cout<< x <<" "<< y <<endl;
x=x+1.5;
} // end of the loop ----------------
TASK 18
Generating numbers of an arithmetic series x
up to the specified maximum value and
computing the value of the function y= f(x)
for these values according to the formula:
𝟓
𝒚 = 𝐬𝐢𝐧(𝒙/𝟑) + √𝒙 + 𝐥𝐨𝐠 𝟏𝟎 𝒙 + √𝒙
Find values x,y for f(x) where is MAXIMUM

Maths model
value x value xmax arithmetic series with difference 0.1
x=0.1 0.2 0.3 … xmax value y = f(x)
𝟓
𝒇(𝒙) = 𝐬𝐢𝐧(𝒙 /𝟑) + √𝒙 + 𝐥𝐨𝐠 𝟏𝟎 𝒙 + √𝒙
value ymax, xm
START
Declaration x, xmax=50, y, ymax, xm
x = 0.1
ymax = f(x) xm=0.1
FALSE
x<=xmax
TRUE
y = f(x)
TRUE
loop

y>ymax ymax =y

FALSE xm = x

x = x +0.1

Show xm, ymax

STOP
double x(0.1),xmax=50, y, xm, ymax; // declaration
ymax=sin(x/3)+sqrt(x) + log10(x)+ pow(x,0.2);
xm=x;
while (x<=xmax) { // start of the loop --------------
y=sin(x/3)+sqrt(x) + log10(x)+ pow(x,0.2);
if (y>ymax) {
ymax=y;
xm=x;
}
x=x+0.1;
} // end of the loop ----------------------------------
cout<<"maximum xm "<<xm<<" ymax "<< ymax<<endl;
TASK 19
Generating numbers of an arithmetic series x
up to the specified maximum value and
computing the value of the function y= f(x)
for these values according to the formula:
𝟓
𝒚 = 𝐬𝐢𝐧(𝒙/𝟑) + √𝒙 + 𝐥𝐨𝐠 𝟏𝟎 𝒙 + √𝒙 − 𝟖
Find value x0 , where f(x) is value 0

Maths model
value x value xmax arithmetic series with difference 0.1
x=0.1 0.2 0.3 … xmax value y = f(x)
𝟓
𝒇(𝒙) = 𝐬𝐢𝐧(𝒙 /𝟑) + √𝒙 + 𝐥𝐨𝐠 𝟏𝟎 𝒙 + √𝒙 − 𝟖
value x0
𝟓 𝟓
𝒇(𝒙) = 𝐬𝐢𝐧(𝒙 /𝟑) + √𝒙 + 𝐥𝐨𝐠 𝟏𝟎 𝒙 + √𝒙 − 𝟖 𝒇(𝒙) = |𝐬𝐢𝐧(𝒙 /𝟑) + √𝒙 + 𝐥𝐨𝐠 𝟏𝟎 𝒙 + √𝒙 − 𝟖|
START
Declaration x, xmax=50, y, x0, ymin
x = 0.1
ymin = |f(x)| x0=0.1
FALSE
x<=xmax
TRUE
y = |f(x)|
TRUE
loop

y<ymin ymin =y

FALSE x x0 = x

x = x +0.1

Show x0, ymin

STOP
double x(0.1), xmax=50, y,x0, ymin; // declaration
ymin=abs(sin(x/3)+sqrt(x) + log10(x)+ pow(x,0.2)-8);
x0=x;
while (x<=xmax) { // start of the loop --------------
y=abs(sin(x/3)+sqrt(x) + log10(x)+ pow(x,0.2)-8);
if (y<ymin) {
ymin=y;
x0=x;
}
x=x+0.1;
} // end of the loop ----------------------------------
cout<<"x0 "<<x0<<" ymin "<< ymin<<endl;
TASK 20
Generating numbers of an arithmetic series x
up to the specified maximum value and
computing the value of the function y= f(x)
for these values according to the formula:
𝟓
𝒚 = 𝐬𝐢𝐧(𝒙/𝟑) + √𝒙 + 𝐥𝐨𝐠 𝟏𝟎 𝒙 + √𝒙
Find the value of the definite integral of the
function f(x) range < 0, xmax >

Maths model
value x value xmax arithmetic series with difference dx=0.1
x=0.1 0.2 0.3 … xmax value y = f(x)
𝟓
𝒇(𝒙) = 𝐬𝐢𝐧(𝒙 /𝟑) + √𝒙 + 𝐥𝐨𝐠 𝟏𝟎 𝒙 + √𝒙
value dx (integral step) value: integral
START
Declaration x, xmax=50, y, dx=0.1, integral
x = 0.1
integral =0
FALSE
x<=xmax
TRUE
y = |f(x)|
loop

integral = integral +y*dx

x = x +dx

Show integral

STOP
double x, xmax, y, dx=0.1, integral;
cout<<" integral 0.1-xmax xmax="; cin>>xmax;
for(integral=0, x=0.1; x<=xmax; x=x+dx) {// start
y=abs(sin(x/3)+sqrt(x) + log10(x)+ pow(x,0.2));
integral = integral + y*dx;
} // end ---------------------------------
cout<<" integral "<< integral <<endl;
Symulations Algorithms
TASK 21
Calculate the position and speed of the vehicle moving at speed v1 and acceleration a.
After the distance d1 has been traversed, the vehicle travels at a constant speed until the
distance d2 has been traversed.
Maths model

t=0 dt=0.1s
v1=0, a=0.5 m/s2
v2 a=0 v3=0, a=0
d1=1000m d2=2000m

t=t+dt d=at2/2 d=d1+v2t


START

Declaration t=0, dt=1 , a=0,5 d1=1000 d2=2000, d, v,


d=0
FALSE
d<=d2
TRUE FALSE
d<=d1
d = d+ v*dt
TRUE
d=at2/2
v=at

Show d, v

t = t +dt

STOP
double t=0, dt=1, v, a=0.5, d1=1000, d2=2000, d;
d=0;
while(d<=d2){ // ***************
if (d<=d1){
d = a*t*t/2;
v = a*t;
}
else{
d = d + v*dt;
}
cout<< " d " << d << " v " << v <<endl;
t=t + dt;
}// ***************
TASK 22
Calculate the position of vehicle A under acceleration aA.
Calculate the position of the vehicle B moving at constant speed vB in the opposite direction
Set a time limit for these vehicles to meet. The dAB distance between A B is 2000m.
Maths model

t=0 dt=0.1s
vA=0, aA=0.2 m/s2 vB= 10 m/s aB=0

t=t+dt dA=at2/2 dB=vbt

dAB - (dA+dB) < 0


START

Declaration t=0, dt=0.1 , a1=0.3 , d1 , d2, va, vb=10 , dab=2000 delta

d1=a1*t2/2
v1=a1*t

d2 = v2*t

delta=dab-(d1+d2)

Show d1, v1, d2, v2 delta


FALSE
delta>0
TRUE
t = t +dt

Show t

STOP
double t=0, dt=0.1, v1, v2=10, a1=0.3, d1, d2, dab=2000, delta;
do {
d1=a1*t*t/2;
v1=a1*t;
d2=v2*t;
delta=dab-d1-d2;
cout<< " d1 "<< d1<< " v1 "<<v1
<< " d2 "<< d2<< " v2 "<<v2
<< " delta "<< delta <<endl;
t=t+dt;
} while(delta>=0);
cout<< " meeting time "<< t <<endl;
TASK 23
Calculate the position of the end of the line with uniformly accelerated rotary motion with
angular acceleration ε = 0.1 rad / s2 around the point (x0, y0) for the time tk = 20s.

Maths model

x0=0
x,y y0=0
x=x0+rcos( φ)
y=y0+rsin( φ)
φ

x0,y0
φ=ε*t2/2

t = t + dt
START

Declaration t=0, dt=0.1 , x, y, eps=0.1 , fi=0 , tk=20 , r=2


FALSE
t<=tk
TRUE
fi=esp*t2/2

x=r*cos(fi)
y=r*sin(fi)

Show t, x, y,

t = t +dt

STOP
#include <iostream>
#include <cmath>
using namespace std;
int main(){// ----------------------------------------
double t=0, dt=0.1, fi, eps=0.1, tk=20,x,y, r=2;
for(t=0; t<=tk; t=t+dt) { //*************
fi=eps*t*t/2;
x=r*cos(fi);
y=r*sin(fi);
cout<< " t "<< t << " x "<< x << " y "<< y <<endl;
}//*************
return 0;
} // ----------------------------------------
TASK 24
Calculate the position (x, y) of the body in projectile
motion when the initial velocity of the thrown body is
v0 = 100m / s and the projection angle is alpha = 30o

Maths model

x = v0 * cos (alfa) * t
t = t + dt
y = v0 * sin (alfa) * t – g * t2 / 2
dt = 0.1s
v0=100m/s
g=10 m/s2
alfa=30o
START

Declaration t=0, dt=0.1 , x, y, g=10 , alfa=30, v0=100


FALSE
y>0
TRUE

x = v0*cos(alfa*PI/180)*t
y= v0*sin(alfa*PI/180)*t-g*t2/2

Show t, x, y,

t = t +dt

STOP
#include <iostream>
#include <cmath>
using namespace std;
int main(){ // ----------------------------------------
double t, dt=0.5, fi, g=10, x, y=0, alfa, v0=100;
cout<< " alfa="; cin>> alfa;
for(t=0; y>=0; t = t + dt) {//*************
x=v0*cos(alfa*M_PI/180)*t;
y=v0*sin(alfa*M_PI/180)*t-g*t*t/2;
cout<< " t " << t << " x "<< x << " y " << y <<endl;
}//*************
return 0;
}// ----------------------------------------

You might also like