16
ENG004 ALGORITHMS & INT. TO PROGRAMMING Week 4 “Arrays and Matrix Operations III” Ahmet Anıl Dindar 21.03.2007

ENG004 ALGORITHMS & INT. TO PROGRAMMING

  • Upload
    rumor

  • View
    50

  • Download
    0

Embed Size (px)

DESCRIPTION

ENG004 ALGORITHMS & INT. TO PROGRAMMING. Week 4 “Arrays and Matrix Operations III” Ahmet Anıl Dindar 21.03.2007. The class facts. E-mail address : [email protected] Class web page: http://web.iku.edu.tr/courses/insaat/eng004/. Last week. First Program! Arrays (Matrices) - PowerPoint PPT Presentation

Citation preview

Page 1: ENG004  ALGORITHMS &  INT. TO PROGRAMMING

ENG004 ALGORITHMS & INT. TO PROGRAMMING

Week 4

“Arrays and Matrix Operations III”

Ahmet Anıl Dindar

21.03.2007

Page 2: ENG004  ALGORITHMS &  INT. TO PROGRAMMING

ENG004.01 2

The class facts

E-mail address: [email protected]

Class web page: http://web.iku.edu.tr/courses/insaat/eng004/

Page 3: ENG004  ALGORITHMS &  INT. TO PROGRAMMING

ENG004.01 3

Last week

First Program! Arrays (Matrices) The user input command : “input” The arrays in formulas Trigonometric calculations Examples:

“Gauss Number summation”

“Fligth Problem”

Page 4: ENG004  ALGORITHMS &  INT. TO PROGRAMMING

ENG004.01 4

This week

More programming... Accessing elements of arrays and matrices Element by element matrix operations Example

“Fligth Problem”

Page 5: ENG004  ALGORITHMS &  INT. TO PROGRAMMING

ENG004.01 5

Fligth Problem

We want to calculate the followings?

1- The duration of the object’s fligth.(Tflight)

2- The highest altitude of the fligth. (ymax)

3- The distance between the origin and landing point. (xmax)

PS: 2 weeks later, we will visulize our findings!

Page 6: ENG004  ALGORITHMS &  INT. TO PROGRAMMING

ENG004.01 6

Fligth ProblemThe formulation of the flight problem is:

20

0

21)()(

)()(

tgtSinVty

tCosVtx

By using this equation we determine the following equations

1- The duration of the object’s fligth.(tflight)

2- The highest altitude of the fligth. (ymax)

3- The distance between the origin and landing point. (xmax)

g

SinVt fligth

)(2 0

20max )2(21)2()( flightflight tgtSinVy

)()(0max flighttCosVx

Page 7: ENG004  ALGORITHMS &  INT. TO PROGRAMMING

ENG004.01 7

Let’s re-write the Fligth Problem codeThe user should enter the initial values into the computer

1- The initial velocity (V0)2- The horizontal angle ()3- The gravity acceleration (g)

Then the code should calculate 1- The duration of the object’s fligth.(Tflight)2- The highest altitude of the fligth. (ymax)3- The distance between the origin and landing point. (xmax)

Page 8: ENG004  ALGORITHMS &  INT. TO PROGRAMMING

ENG004.01 8

The Code of the Flight Problem

clc,clear,close all

v0=input('Please enter the initial velocity : ')alfa=input('Pleae enter the horizontal angle : ')g=input('Please enter the gravity acceleration : ')

The user input lines

The computer calculations

% The following line calculates the duration of the fligthtflight=2*v0*sin(alfa*pi/180)/g ;

Page 9: ENG004  ALGORITHMS &  INT. TO PROGRAMMING

ENG004.01 9

The Code of the Flight Problem

% xmax xmax=v0*cos(alfa*pi/180)*tfligth;% ymaxymax=v0*sin(alfa*pi/180)*tf/2-.5*g*(tf/2)^2;

The computer should calculate the xmax and ymax.

The “;” does not allow the program to display the results in the command window.If you want to see the results, you need new commands : “fpritnf”

% let's see the results in the command windowfprintf('The highest altitude of the flight is %.2f m \n',ymax)fprintf('The distance of the landing point is %.2f m \n',xmax)

Page 10: ENG004  ALGORITHMS &  INT. TO PROGRAMMING

ENG004.01 10

The Code of the Flight ProblemLet’s estimate the max values from the x and y series.In order to create the x and y series, we need to create the time series:

% time seriest=(0:.1:tf);

We use the formulations for the x and y series

% now let's calculate the x and y seriesx=v0*cos(alfa*pi/180)*t;y=v0*sin(alfa*pi/180)*t-.5*g*(t).^2;

20

0

21)()(

)()(

tgtSinVty

tCosVtx

Think of the “.”

Page 11: ENG004  ALGORITHMS &  INT. TO PROGRAMMING

ENG004.01 11

The Code of the Flight ProblemLet’s estimate the max values from the x and y series.In order to create the x and y series, we need to create the time series:

% min and max commandsxmax2=max(x);ymax2=max(y);

% let's see the results in the command windowfprintf('The highest altitude of the flight is %.2f m \n',ymax2)fprintf('The distance of the landing point is %.2f m \n',xmax2)

Page 12: ENG004  ALGORITHMS &  INT. TO PROGRAMMING

ENG004.01 12

The Code of the Flight ProblemLet’s run the program and see the results!

clc,clear,close allv0=input('Please enter the initial velocity : ')alfa=input('Pleae enter the horizontal angle : ')g=input('Please enter the gravitaty acceleration : ')% The following line calculates the duration of the fligthtf=2*v0*sin(alfa*pi/180)/g;% max distancexmax=v0*cos(alfa*pi/180)*tf;% ymaxymax=v0*sin(alfa*pi/180)*tf/2-.5*g*(tf/2)^2;% let's see the results in the command windowfprintf('The highest altitude of the flight is %.2f m \n',ymax)fprintf('The distance of the landing point is %.2f m \n',xmax)% time seriest=(0:.1:tf);% now let's calculate the x and y seriesx=v0*cos(alfa*pi/180)*t;y=v0*sin(alfa*pi/180)*t-.5*g*(t).^2;% min and max commandsxmax2=max(x);ymax2=max(y);fprintf('The highest altitude of the flight is %.2f m (by using alternative way)\n',ymax2)fprintf('The distance of the landing point is %.2f m (by using alternative way)\n',xmax2)

Page 13: ENG004  ALGORITHMS &  INT. TO PROGRAMMING

ENG004.01 13

Assignment 3Let’s create a vector matrix (x) and perform the following formulation.

f(x)=variable1*x^3+variable2*x^2-2*x

And estimate the min and max values.

Page 14: ENG004  ALGORITHMS &  INT. TO PROGRAMMING

ENG004.01 14

Send your diary files to class e-mail

Page 15: ENG004  ALGORITHMS &  INT. TO PROGRAMMING

ENG004.01 15

Next week...

Weeks Date Class Assignment

1 21.02 Introduction to the class Registration

2 28.02 Exploring MATLAB Assignment 1

3 07.03 Arrays and Matrix Operations N.A.

4 14.03 Arrays and Matrix Operations Assignment 2

5 21.03 Arrays and Matrix Operations Assignment 3

6 28.03 Polynomials and curve-fitting Assignment 4

7 04.04 Descriptive statistics Assignment 5

8 11.04 Programming Elements of MATLAB N.A.

9 18.04 Programming Elements of MATLAB Assignment 6

10 25.04 Programming Elements of MATLAB Assignment 7

11 02.05 Plotting and Animations N.A.

12 09.05 Plotting and Animations Assignment 8

13 16.05 Numerical Integration Assignment 9

14 23.05 Symbolic Math Assignment 10

15 30.05 Take-home final project N.A.

Page 16: ENG004  ALGORITHMS &  INT. TO PROGRAMMING

ENG004.01 16

See you next week!