18
Ali Can ÖZER 1631985 INTRODUCTION For one degree differential equations, Euler’s Method is applicable to so them in an approximate manner. In my approach,I consider a car that produce same force to move,there is drag that produce resistance in the opposite direction of the thrust.and have a C d !rag Coefficient", determined mass and air density. #he values of coefficients and are are ta$en from internet as average val From %e&ton’s 'econd (a& of Mation F)ma,equations &ere determined. In the other parts of the calculations Improved Euler’s Equation*eun’s Method",+unge $utta Method &ill be used. METHOD AND CODES -eneral !rag Formula D = 0.5ρ V 2 C d A ρ !ensity V /elocity C d !rag coefficient A area %e&ton’s second la& of motion F = m a= m dV dt 0ur ordinary differential equation0!E" becomes F D = m dV dt F 0.5ρ V 2 C d A = m dV dt 1

Numeric Hw1 Report

Embed Size (px)

DESCRIPTION

numerical methods hw

Citation preview

Ali Can ZER1631985

INTRODUCTION

For one degree differential equations, Eulers Method is applicable to solve them in an approximate manner. In my approach,I consider a car that produce same force to move,there is a drag that produce resistance in the opposite direction of the thrust.and have a constant Cd(Drag Coefficient), determined mass and air density.The values of coefficients and are are taken from internet as average values.From Newtons Second Law of Mation F=ma,equations were determined.In the other parts of the calculations Improved Eulers Equation(Heuns Method),Runge-kutta Method will be used.

METHOD AND CODES

General Drag Formula:

: Density : Velocity : Drag coefficient : area

Newtons second law of motion :

Our ordinary differential equation(ODE) becomes:

The reason that Heuns Method is called improved Eulers method,it gives more precise results than Eulers because in this process the two succesive slopes are taken into account which results smaller errors.For Runge-Kutta method we determine the ratios of the slopes that we use. Morover,it also gives us the chance of reducing the stepsize more by controlling its multiplier. It may give better approximations.

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 four other values, which are a1, a2 ,p1 and p2 but they have to satisfy that conditions :

and

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..A SIMPLE EULER SOLVER c------------------------------------------------------------------- program euler character*40 fname

c..Read the solution parameters print*, ' ' print*, ' Enter StepSize and FinalTime :> ' read(*,*) d, 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. v = 0.

write(1,'(2f12.3)') time, v

c..Solution loop (d=stepsize) do while ( time .lt. finaltime ) time = time + d slope = ODE(v) v = v + d*slope write(1,'(2f12.3)') time, v enddo

c..Close the output file close(1)

stop end

c-------------------------------------------------------------------c..Define the ODE as a function function ODE(vel) data Th/4000/, ro/1.225/, xmass/1200/,cds/0.63/

ODE = (Th-0.5*ro*(vel**2)*cds)/xmass

return end

c-------------------------------------------------------------------c..HEUNS METHODc------------------------------------------------------------------- program HEUN character*40 fname

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

c..open the output file print*,' Enter the output file name [HEUN.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. v = 0. write(1,'(2f12.3)') time, v

c..Solution loop do while ( time .lt. finaltime ) time = time + d slope1 = ODE(v) v1= v+d*slope1 slope2= ODE(v1) slope=(slope1+slope2)/2 v = v + d*slope write(1,'(2f12.3)') time, v enddo

c..Close the output file close(1)

stop end

c-------------------------------------------------------------------c..Define the ODE as a function function ODE(vel) data Th/4000/, ro/1.225/, xmass/1200/,cds/0.63/

ODE = (Th-0.5*ro*(vel**2)*cds)/xmass

return end

c..Runge-Kutta Method Solverc------------------------------------------------------------------- program EULER character*40 fname

c..Read the solution parameters print*, ' ' print*, ' Enter StepSize and FinalTime and a2 :> ' read(*,*) d, finaltime,a2c.. If we put a2=0.5 it will give Heuns method

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. v = 0. a1=1.0-a2 p1=0.5/a2 write(1,'(2f12.3)') time, v

c..Solution loop do while ( time .lt. finaltime ) time = time + d slope1 = ODE(v) v1= v+d*slope1*p1 slope2= ODE(v1) slope=slope1*a1+slope2*a2 v = v + d*slope write(1,'(2f12.3)') time, v enddo

c..Close the output file close(1)

stop end

c-------------------------------------------------------------------c..Define the ODE as a function function ODE(vel) data Th/4000/, ro/1.225/, xmass/1200/,cds/0.63/

ODE = (Th-0.5*ro*(vel**2)*cds)/xmass

return end

c..Error of Second Order Runge-Kutta for ODEsc------------------------------------------------------------------- program RK character*40 fname

c..Read the solution parameters print*, ' ' print*, ' Enter StepSize and FinalTime and a2 :> ' read(*,*) d, finaltime,a2c.. If we put a2=0.5 it will give Heuns method

c..open the output file print*,' Enter the output file name [RKerror2.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. v = 0. a1=1.0-a2 p1=0.5/a2 write(1,'(2f12.3)') time, v

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

time = time + d slope1 = ODE(v) v1= v+d*slope1*p1 slope2= ODE(v1) slope=slope1*a1+slope2*a2 v = v + d*slope ex=(1.825*(exp(0.0653*time)-1))/(0.0179*(exp(0.0653*time)+1)) err=abs((abs(ex)-abs(v))/ex)

write(1,'(3f12.3)') time,err,d

enddo d=d+0.1 enddo

c..Close the output file close(1) stop endc-------------------------------------------------------------------c..Define the ODE as a function function ODE(vel) data Th/4000/, ro/1.225/, xmass/1200/,cds/0.63/ ODE = (Th-0.5*ro*(vel**2)*cds)/xmass return endc-------------------------------------------------------------------c..Determination of Maximum Step Sizec------------------------------------------------------------------- program RK character*40 fname

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

c..open the output file print*,' Enter the output file name [Maxstep.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. v = 0. a1=1.0-a2 p1=0.5/a2 write(1,'(2f12.3)') time, v

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

time = time + d slope1 = ODE(v) v1= v+d*slope1*p1 slope2= ODE(v1) slope=slope1*a1+slope2*a2 v = v + d*slope ex=(1.825*(exp(0.0653*time)-1))/(0.0179*(exp(0.0653*time)+1)) err=abs((abs(ex)-abs(v))/ex)

write(1,'(3f12.3)') time,err,d

enddo d=d+0.1 enddo

c..Close the output file close(1)

stop endc-------------------------------------------------------------------c..Define the ODE as a function function ODE(vel) data Th/4000/, ro/1.225/, xmass/1200/,cds/0.63/ ODE = (Th-0.5*ro*(vel**2)*cds)/xmass return end program EXACT VALUE character*40 fname print*, 'Enter Stepsize and final time :> ' read(*,*) d,ft print*, ' Output file name [exavel.dat] :' read(*,'(a)') fname if(fname .eq. ' ') fname = 'exavel.dat' open(1,file=fname,form='formatted') t= 0. v=0. do while (t .lt. ft) t=t+d v=(1.825*(exp(0.0653*t)-1))/(0.0179*(exp(0.0653*t)+1)) write(1,'(2f12.3)') t,v enddo stop end

--From the maximum Stepsize code, at stepsize=54.1,function diverges and after that it starts to give infinite values. That shows 54.0 is the largest stepsize for Improved Euler Equations.

DISCUSSIONFrom the graphs, it can be seen that improved Eulers method and Runge-Kutta Method are more precise than Eulers method itself.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.

1