https://github.com/rafifiaan/p1-numericalcomputation-matlab-py
Bisection Method Program - Numerical Computation Course
https://github.com/rafifiaan/p1-numericalcomputation-matlab-py
bisection-method numerical-computation python
Last synced: 4 months ago
JSON representation
Bisection Method Program - Numerical Computation Course
- Host: GitHub
- URL: https://github.com/rafifiaan/p1-numericalcomputation-matlab-py
- Owner: rafifiaan
- Created: 2022-09-14T13:30:50.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2022-10-26T08:40:27.000Z (over 2 years ago)
- Last Synced: 2025-02-25T20:18:25.564Z (4 months ago)
- Topics: bisection-method, numerical-computation, python
- Language: MATLAB
- Homepage:
- Size: 13.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Practicum 1 - Numerical Computation
Name : Rafi Aliefian Putra Ramadhani
NRP : 5025211234## Source Code with MATLAB
>Doing execution Bisection Method using MATLAB Language (Platform)
- Source :
```R
% Input
% fungsi non linier yang didefinisikan dalam mfile f.m
% batas atas dan batas bawah
% galat toleransi
%
% Output
% iterasi, solusi dan galat
%
% contoh
% x.^3-3*x+1
% Masukkan batas bawah :0
% Masukkan batas atas :5
% Masukkan galat Toleransi :0.0001clear
clc
disp(' Metode Bolzano')
disp(' Tekan Enter untuk lanjut')
pause
clc%% Input nilai interval dan galat
disp('x.^3-3*x+1')
a=input('Masukkan batas bawah :');
b=input('Masukkan batas atas :');
galat1=input('Masukkan galat Toleransi :');%% Ganti fungsinya di bawah ini
f= @(x) x.^3-3*x+1;%% Algoritma
init=[a,b];
ya=f(a) ; %Nilai f(a)
yb=f(b); %Nilai f(b)
iter=0;
xm1=a;
galat=abs((b-a)/b);while ya*yb>0
disp('Ingat f(a)*f(b)>0, berarti tidak ada akar dalam selang'),
break,
endfprintf('\n iterasi a b xm f(xm) Galat\n');
while galat>galat1
iter=iter+1;
xm=(a+b)/2;
yxm=f(xm);
a1=a;b1=b; % Inisialisasi untuk Tampilan
while yxm==0, break,
end
if ya*yxm<0
b=xm;
yb=yxm;
else
a=xm;
ya=yxm;
end
galat=abs((xm-xm1)/xm);
xm1=xm;
fprintf('%10.0f %6.10f %6.10f %6.10f %6.10f %6.10f\n',[iter;a1;b1;xm;yxm;galat])
end
fprintf('Akarnya adalah = %6.10f\n',xm)
xx=linspace(init(1),init(2),100);
yy=f(xx);%% Plot kurva dan titik estimasi
figure(1)
plot(xx,yy)
hold on
scatter(xm1,f(xm1),'filled','p')
plot(xx,zeros(length(xx),1))
xlabel('x')
ylabel('f(x)')
legend('f(x)','akar persamaan','garis f(x)=0')
title(sprintf('Metode Bolzano - Akarnya adalah = %.8f', xm1))
```
- Result :
## Source Code with Python
>Doing execution Bisection Method using Python Language (Online Compiler Platform)
- Source :
```R
import numpy as np
import matplotlib.pyplot as pltprint("Metode Bolzano - Numerical Computation")
print("-----------------------------------------")
persamaan = input("Masukkan Persamaan fungsi x : ")
a = float(input("Masukkan nilai a : "))
b = float(input("Masukkan nilai b : "))
e = 1e-5
print("Akar persamaan dari fungsi x : ", persamaan, " dengan batas bawah : ", a, " dan batas atas : ", b)def f(x):
return eval(persamaan)if f(a)*f(b)>0:
print("Persamaan tidak memiliki akar")
else :
print("n a b x f(a) f(b) f(x)")
for i in range(0,300):
x = (a+b)/2
print(i+1, "\t", format(a,".5f"), "\t", format(b,".5f"), "\t", format(x, ".5f"), "\t", format(f(a),".5f"), "\t", format(f(b),".5f"), "\t", format(f(x),".5f"))
if abs(f(x))