Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Numeric Analysis Engineering, Assignments of Mathematical Methods for Numerical Analysis and Optimization

euler method modified euler method runge kutte

Typology: Assignments

2023/2024

Uploaded on 03/26/2024

unknown user
unknown user 🇹🇷

1 document

1 / 9

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
# -*- coding: utf-8 -*-
"""
Created on Fri Mar 15 15:15:44 2024
@author:
"""
import numpy as np
from matplotlib import pyplot as plt
import math
# u'=0.5(x-u)
# i-) Euler Yöntemi; h= 0.25 ve h=0.1; 0<=x<=1
# u(0)=1
# u(x+h)=u(x)+u'(x)*h
# h=0.25 için
h1=0.25
x= np.arange(0,1.00001,h1)
u = np.zeros(len(x))
u[0]=1
for i in range(0, len(x) - 1):
u[i + 1] = u[i] + h1*0.5*(x[i]-u[i])
print("")
print('_' * 30)
print("")
print("EULER YÖNTEMİ")
print("")
print("")
print("h=0.25 için")
print("")
for i in range(len(u)):
print("u(",x[i],")=",u[i])
print("")
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Numeric Analysis Engineering and more Assignments Mathematical Methods for Numerical Analysis and Optimization in PDF only on Docsity!

-- coding: utf-8 --

""" Created on Fri Mar 15 15:15:44 2024 @author: """ import numpy as np from matplotlib import pyplot as plt import math

u'=0.5(x-u)

i-) Euler Yöntemi; h= 0.25 ve h=0.1; 0<=x<=

u(0)=

u(x+h)=u(x)+u'(x)*h

h=0.25 için

h1=0. x= np.arange(0,1.00001,h1) u = np.zeros(len(x)) u[0]= for i in range(0, len(x) - 1): u[i + 1] = u[i] + h10.5(x[i]-u[i]) print("") print('_' * 30) print("") print("EULER YÖNTEMİ") print("") print("") print("h=0.25 için") print("") for i in range(len(u)): print("u(",x[i],")=",u[i]) print("")

print('_' * 30) print("") plt.plot(x, u,'g-') plt.grid plt.xlabel('x values') plt.ylabel('u values') plt.title('EULER YÖNTEMİ-h=0.25') plt.tight_layout() plt.show()

h=0.1 için

h2=0. x1= np.arange(0,1.00001,h2) u1 = np.zeros(len(x1)) u1[0]= for i in range(0, len(x1) - 1): u1[i + 1] = u1[i] + h20.5(x1[i]-u1[i]) print("") print('' * 30) print("") print("EULER YÖNTEMİ") print("") print("") print("h=0.1 için") print("") for i in range(len(u1)): print("u(",round(x1[i],2),")=",u1[i]) print("") print('' * 30) print("") plt.plot(x1, u1,'g-') plt.grid plt.xlabel('x values')

print("h=0.25 için") print("") for i in range(len(u3)): print("u(",x[i],")=",u3[i]) print("") print('_' * 30) print("") plt.plot(x, u3,'g-') plt.grid plt.xlabel('x values') plt.ylabel('u values') plt.title('MODİFİYE EULER YÖNTEMİ-h=0.25') plt.tight_layout() plt.show()

h=0.1 için

u4 = np.zeros(len(x1)) u4[0]= k_1=np.zeros(len(x1)) k_2=np.zeros(len(x1)) k_1[0]=0.5(x1[0]-u4[0]) for i in range(0, len(x1) - 1): k_1[i]=0.5(x1[i]-u4[i]) k_2[i]=((h2+x1[i])-(u4[i]+h2k_1[i]))0. u4[i+1]=u4[i]+0.5h2(k_1[i]+k_2[i]) k_1[i+1]=k_2[i] print("") print('_' * 30) print("") print("MODİFİYE EULER YÖNTEMİ") print("") print("") print("h=0.1 için")

print("") for i in range(len(u4)): print("u(",round(x1[i],2),")=",u4[i]) print("") print('_' * 30) print("") plt.plot(x1, u4,'g-') plt.grid plt.xlabel('x values') plt.ylabel('u values') plt.title('MODİFİYE EULER YÖNTEMİ-h=0.1') plt.tight_layout() plt.show()

aynı grafikte

plt.plot(x, u3,label='h=0.25 için') plt.plot(x1, u4, label='h=0.1 için') plt.grid plt.xlabel('x values') plt.ylabel('u values') plt.title('MODİFİYE EULER YÖNTEMİ-h=0.25 ve h=0.1 için') plt.tight_layout() plt.legend() plt.show()

iii-) Dördüncü Derece Runge-Kutta Yöntemi; h= 0.25 ve h=0.

h=0.25 için

x= np.arange(0,1.00001,h1) u5 = np.zeros(len(x)) u5[0]= k11=np.zeros(len(x)) k22=np.zeros(len(x)) k33=np.zeros(len(x)) k44=np.zeros(len(x))

k2_2=np.zeros(len(x1)) k3_3=np.zeros(len(x1)) k4_4=np.zeros(len(x1)) for i in range(0, len(x1) - 1): k1_1[i]=0.5(x1[i]-u6[i]) k2_2[i]=((0.5h2+x1[i])-(u6[i]+0.5h2k1_1[i]))0. k3_3[i]=((0.5h2+x1[i])-(u6[i]+0.5h2k2_2[i]))0. k4_4[i]=0.5((x1[i]+h2)-(u6[i]+k3_3[i]h2)) u6[i+1]=u6[i]+(k1_1[i]+2k2_2[i]+2k3_3[i]+k4_4[i])h2/ print("") print('' * 30) print("") print("Dördüncü Derece Runge-Kutta Yöntemi") print("") print("") print("h=0.1 için") print("") for i in range(len(u6)): print("u(",round(x1[i],2),")=",u6[i]) print("") print('' * 30) print("") plt.plot(x1, u6,'g-') plt.grid plt.xlabel('x values') plt.ylabel('u values') plt.title('Dördüncü Derece Runge-Kutta Yöntemi-h=0.1') plt.tight_layout() plt.show()

aynı grafikte

plt.plot(x, u5,label='h=0.25 için')

plt.plot(x1, u6, label='h=0.1 için') plt.grid plt.xlabel('x values') plt.ylabel('u values') plt.title('4. Derece Runge-Kutta Yöntemi-h=0.25 ve h=0.1 için') plt.tight_layout() plt.legend() plt.show() #2.soru def dvdt(t,v,fs,g): return (-vfs-g) def rungeKutta(t0, v0, t, h,fs,g): n = (int((t - t0)/h)) v = v for i in range(1, n + 1): k1 = h * dvdt(t0,v,fs,g) k2 = h * dvdt(t0 + 0.5 * h, v + 0.5 * k1,fs,g) k3 = h * dvdt(t0 + 0.5 * h, v + 0.5 * k2,fs,g) k4 = h * dvdt(t0 + h, v + k3,fs,g) v = v + (1.0 / 6.0)(k1 + 2 * k2 + 2 * k3 + k4) t0 = t0 + h return v

İstediğin değeri gir