160 likes | 235 Views
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 : iku.eng004.01@gmail.com Class web page: http://web.iku.edu.tr/courses/insaat/eng004/. Last week. First Program! Arrays (Matrices)
E N D
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: iku.eng004.01@gmail.com • Class web page: http://web.iku.edu.tr/courses/insaat/eng004/ ENG004.01
Last week • First Program! • Arrays (Matrices) • The user input command : “input” • The arrays in formulas • Trigonometric calculations • Examples: “Gauss Number summation” “Fligth Problem” ENG004.01
This week • More programming... • Accessing elements of arrays and matrices • Element by element matrix operations • Example “Fligth Problem” ENG004.01
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! ENG004.01
Fligth Problem The formulation of the flight problem is: 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) ENG004.01
Let’s re-write the Fligth Problem code The user should enter the initial values into the computer 1- The initial velocity (V0) 2- The horizontal angle (a) 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) If OK, Let's start writing the CODE! ENG004.01
The user input lines 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 computer calculations % The following line calculates the duration of the fligth tflight=2*v0*sin(alfa*pi/180)/g ; Now, Do you think you can calculate the ymax and xmax? ENG004.01
The computer should calculate the xmax and ymax. The Code of the Flight Problem % xmax xmax=v0*cos(alfa*pi/180)*tfligth; % ymax ymax=v0*sin(alfa*pi/180)*tf/2-.5*g*(tf/2)^2; 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 window fprintf('The highest altitude of the flight is %.2f m \n',ymax) fprintf('The distance of the landing point is %.2f m \n',xmax) ENG004.01
The Code of the Flight Problem Let’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 series t=(0:.1:tf); We use the formulations for the x and y series Think of the “.” % now let's calculate the x and y series x=v0*cos(alfa*pi/180)*t; y=v0*sin(alfa*pi/180)*t-.5*g*(t).^2; ENG004.01
The Code of the Flight Problem Let’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 commands xmax2=max(x); ymax2=max(y); % let's see the results in the command window fprintf('The highest altitude of the flight is %.2f m \n',ymax2) fprintf('The distance of the landing point is %.2f m \n',xmax2) ENG004.01
The Code of the Flight Problem Let’s run the program and see the results! clc,clear,close all v0=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 fligth tf=2*v0*sin(alfa*pi/180)/g; % max distance xmax=v0*cos(alfa*pi/180)*tf; % ymax ymax=v0*sin(alfa*pi/180)*tf/2-.5*g*(tf/2)^2; % let's see the results in the command window fprintf('The highest altitude of the flight is %.2f m \n',ymax) fprintf('The distance of the landing point is %.2f m \n',xmax) % time series t=(0:.1:tf); % now let's calculate the x and y series x=v0*cos(alfa*pi/180)*t; y=v0*sin(alfa*pi/180)*t-.5*g*(t).^2; % min and max commands xmax2=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) ENG004.01
Assignment 3 Let’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. ENG004.01
Send your diary files to class e-mail ENG004.01
Next week... ENG004.01
See you next week! ENG004.01