Hw1

18
Name : Mehmet Erdoğan 1679778 Section -1 AEE-305 HW 1 IN!"#$%I"N: For ordinary diferential equations we can nd the solution using Euler’s Method in approximate manner. In my homework I consider a car which moves with constant drag coe cient. !hile this car moving there will "e drag #orces we can calculate these drag #orces #rom the #ormula as #ollows D = 0.5ρ V 2 C d A !here $ ρ : Density V : Velocity C d : Drag coefficient A : area In this homework newton’s second law o# motion is used $ F = m a= m dV dt %hen ordinary diferential equation "ecomes $ F D = m dV dt F 0.5 ρ V 2 C d A = m dV dt ODE ( V ) = dV dt = F 0.5 ρ V 2 C d A m he method : In &eun’s method the more precise result is get than Euler’s me "ecause we take into account two succesive slopes then it makes error smaller.

description

num ae305

Transcript of Hw1

Name : Mehmet Erdoan1679778Section -1

AEE-305 HW 1INTRODUCTION: For ordinary differential equations we can find the solution using Eulers Method in approximate manner. In my homework I consider a car which moves with constant drag coefficient. While this car moving there will be drag forces we can calculate these drag forces from the formula as follows:

Where : : Density : Velocity : Drag coefficient : area

In this homework newtons second law of motion is used : Then ordinary differential equation becomes :

The method :In Heuns method the more precise result is get than Eulers method because we take into account two succesive slopes then it makes error smaller.In second order Runge Kutta method we determine the weights of the slopes we used it also gives us chance to reduce step size using that we may have more accurate solution.

Then we define a temprorary velocity by Eulers method :

(Eulers method)

Second slope is:

Average slope becomes:

Iteration function becomes:

For second order Runge-Kutta method we have three other inputs, which are a1, a2 and p1 . The relationship between these is as follows:

In Heuns method we define two variables,k1 and k2 which are represents two different slopes of successive points:

It can be seen that if we choose a2=0.5 then the Runge-Kutta becomes Heuns method.

In order to understand the theory behind Eulers Method,Heuns Method, and Runge Kutta Method more, codes that are below can be examined.

c-------------------------------------------------------------------c..AN EULER SOLVER for ODEs - AE305 Numerical Methods c------------------------------------------------------------------- program EULER character*40 fname c..Read the solution parameters print*, ' ' print*, ' Enter StepSize and FinalTime :> ' read(*,*) stepsize, finaltime c..open the output file print*,' Enter the output file name [velocity.dat]:' read(*,'(a)') fname if( fname .eq. ' ') fname = 'velocity.dat' open(1,file=fname,form='formatted') c..Set the Initial Conditions and output them time = 0. velocity = 0. write(1,'(2f12.3)') time, velocity c..Solution loop do while ( time .lt. finaltime ) velocity = velocity + stepsize*ODE(velocity) time = time + stepsize write(1,'(2f12.3)') time, velocity enddo c..Close the output file close(1) stop end c-------------------------------------------------------------------c..Define the ODE as a Fortran function function ODE(vel) data rho/1.225/, cda/0.63/, xmass/1200/,thrust/4000/ ODE=(thrust - 0.5*rho*(vel**2)*CdA)/xmass return end

c-------------------------------------------------------------------c..A HEUN SOLVER for ODEs - AE305 Numerical Methods c------------------------------------------------------------------- program HEUN character*40 fname c..Read the solution parameters print*, ' ' print*, ' Enter StepSize and FinalTime :> ' read(*,*) stepsize, finaltime c..open the output file print*,' Enter the output file name [velocity.dat]:' read(*,'(a)') fname if( fname .eq. ' ') fname = 'velocity.dat' open(1,file=fname,form='formatted') c..Set the Initial Conditions and output them time = 0. velocity = 0. write(1,'(2f12.3)') time, velocity c..Solution loop do while ( time .lt. finaltime )xk1=ODE(velocity)veleuler=velocity+stepsize*xk1time=time+stepsizexk2=ODE(veleuler)velocity=velocity + stepsize*(xk1+xk2)*0.5 write(1,'(2f12.3)') time, velocity enddo c..Close the output file close(1) stop end c-------------------------------------------------------------------c..Define the ODE as a Fortran function function ODE(vel) data rho/1.225/, cda/0.63/, xmass/1200/,thrust/4000/ ODE=(thrust - 0.5*rho*(vel**2)*CdA)/xmass return end

c-------------------------------------------------------------------c..A RK2 SOLVER for ODEs - AE305 Numerical Methods c------------------------------------------------------------------- program RK2 character*40 fname c..Read the solution parameters print*, ' ' print*, ' Enter StepSize and FinalTime :> ' read(*,*) stepsize, finaltime, a2 c..open the output file print*,' Enter the output file name [velocity.dat]:' read(*,'(a)') fname if( fname .eq. ' ') fname = 'velocity.dat' open(1,file=fname,form='formatted') c..Set the Initial Conditions and output them time = 0. velocity = 0.a1=1-a2p1=0.5/a2 write(1,'(2f12.3)') time, velocity c..Solution loop do while ( time .lt. finaltime )xk1=ODE(velocity)veleuler=velocity+stepsize*xk1*p1time=time+stepsizexk2=ODE(veleuler) velocity=velocity + (xk1*a1+xk2*a2)*stepsize

write(1,'(2f12.3)') time, velocity enddo c..Close the output file close(1) stop end c-------------------------------------------------------------------c..Define the ODE as a Fortran function function ODE(vel) data rho/1.225/, cda/0.63/, xmass/1200/,thrust/4000/ ODE=(thrust - 0.5*rho*(vel**2)*CdA)/xmass return end

The Error Determination :

c-------------------------------------------------------------------c..error of second order runge kutta - AE305 Numerical Methodsc------------------------------------------------------------------- program RK2 character*40 fname

c..Read the solution parameters print*, ' ' print*, ' Enter StepSize and FinalTime and a2:> ' read(*,*) stepsize, finaltime, a2

c..open the output file print*,' Enter the output file name [rk2error.dat]:' read(*,'(a)') fname if( fname .eq. ' ') fname = 'rk2error.dat' open(1,file=fname,form='formatted')

c..Set the Initial Conditions and output them time = 0. velocity = 0.a1=1-a2p1=0.5/a2 write(1,'(2f12.3)') time, velocity

c..Solution loop do while ( stepsize.lt. 1500) do while ( stepsize .lt. 1500) time=0 v=0

do while ( time .lt. finaltime )

xk1=ODE(velocity)veleuler=velocity+stepsize*xk1*p1xk2=ODE(veleuler) velocity=velocity + (xk1*a1+xk2*a2)*stepsize ex=(1.825*(exp(0.0653*time)-1))/(0.0179*(exp(0.0653*time)+1))

err=abs((abs(ex)-abs(velocity))/ex) write(1,'(3f12.3)') time, err, stepsize

enddo stepsize=stepsize+0.1 end doc..Close the output file close(1) stop endc-------------------------------------------------------------------c..Define the ODE as a Fortran function function ODE(vel) data rho/1.225/, cda/0.63/, xmass/1200/,thrust/4000/

ODE=(thrust - 0.5*rho*(vel**2)*CdA)/xmass

return endc-------------------------------------------------------------------c..DETERMINATION OF MAX STEPSZIE - AE305 Numerical Methods c------------------------------------------------------------------- program RK2 character*40 fname c..Read the solution parameters print*, ' ' print*, ' Enter StepSize and FinalTime :> ' read(*,*) stepsize, finaltime, a2 c..open the output file print*,' Enter the output file name [MAXSTEP.dat]:' read(*,'(a)') fname if( fname .eq. ' ') fname = 'MAXSTEP.dat' open(1,file=fname,form='formatted') c..Set the Initial Conditions and output them time = 0. velocity = 0.a1=1-a2p1=0.5/a2 write(1,'(2f12.3)') time, velocity c..Solution loop do while ( stepsize .lt. 1500) time=0 velocity=0

do while ( time .lt. finaltime )xk1=ODE(velocity)veleuler=velocity+stepsize*xk1*p1time=time+stepsizexk2=ODE(veleuler) velocity=velocity + (xk1*a1+xk2*a2)*stepsize ex=(1.825*(exp(0.0653*time)-1))/(0.0179*(exp(0.0653*time)+1)) err=abs((abs(ex)-abs(velocity))/ex)

write(1,'(3f12.3)') time, err, stepsize enddo stepsize=stepsize+0.1end doc..Close the output file close(1) stop end c-------------------------------------------------------------------c..Define the ODE as a Fortran function function ODE(vel) data rho/1.225/, cda/0.63/, xmass/1200/,thrust/4000/ ODE=(thrust - 0.5*rho*(vel**2)*CdA)/xmass return end

program EXACT VALUE character*40 fname print*, 'Enter Stepsize and final time :> ' read(*,*) stepsize,finaltime print*, ' Output file name [exvel.dat] :' read(*,'(a)') fname if(fname .eq. ' ') fname = 'exvel.dat' open(1,file=fname,form='formatted') ti= 0. velocity=0. do while (ti .lt. finaltime) ti=ti+stepsize v=(1.825*(exp(0.0653*ti)-1))/(0.0179*(exp(0.0653*ti)+1)) write(1,'(2f12.3)') ti,velocity enddo stop end

CONCLUSONS :I undertstand from the stepsize graph that 54.0 is the largest stepsize for improved Euler equations. From the graphs, it can be seen that improved Eulers method and Runge-Kutta Method gives more accurate results than euler.When we are talking about Eulers method and improved ones, it is clear that step size is a very important variable that has to be set properly. Choosing small step size result in more calculations, more time and more money. In this case we have not seen any effect of it but in more complicated problems, time will be one of the our concerns. On the other hand choosing large step size will result in more error. As an engineer we should decide a proper value that is good enough but not more than enough. In this problem we have a chance to solve equation and obtain the exact result, however in a real case such application will not be available. If it is available then it is unnecessary to construct Eulers Methods. In this problem we compared the results, and we made sure that our approximation is good. In real applications, we should use different methods, plot much more graphs , compare all of the approximations and apply the approximation method for a solvable equation ,like we have done , and decide that whether it is applicable or not.