Sei sulla pagina 1di 4

Bisection

a = input('Enter value of a. ');


b = input('Enter value of b. ');
tol = input('Enter tolerance. ');
F = inline('0.05-(x/(1-x))*(6/(2+x))^0.5');
while abs(F(a) - F(b)) >= tol
c = (a+b)/2;
if F(a)*F(c) < 0
b = c;
elseif F(b)*F(c) < 0
a = c;
elseif F(c) == 0
break
end
i = i+1;
end
fprintf('%.12f\n', c);
Fixed Point Iteration
init = input('Enter initial guess. ');
tol = input('Enter tolerance. ');
F = inline('log2(x^2 + 2)');
I = [];
xx = [];
x = init;
x1 = 0;
i = 0;
while abs(x - x1) >= tol
x = x1;
x1 = F(x);
i = i+1;
I = [I i];
xx = [xx x1];
end
fprintf('%.12f\n', x);
disp([I' xx']);
Numerical Integration
F = inline('1 - exp(-2*x)');
X = linspace(0,4,1000);
area = 0;
area1 = 0;
area2 = 0;
for i = 1:length(X)-1 %Trapezoidal
area = area + (X(i+1) - X(i))/2 * (F(X(i)) + F(X(i+1)));
end
disp(area);
for i = 1:2:length(X)-2 %Simpsons 1/3
area1 = area1 + (X(i+2) - X(i))/6 * (F(X(i)) + 4*F(X(i+1)) +
F(X(i+2)));

end
disp(area1);
for i = 1:3:length(X)-3 %Simpsons 3/8
area2 = area2 + (X(i+3) - X(i))/8 * (F(X(i)) + 3*F(X(i+1)) +
3*F(X(i+2)) + F(X(i+3)));
end
disp(area2);
quad(F,0,4);

Regula Falsi
a = input('Enter value of a. ');
b = input('Enter value of b. ');
tol = input('Enter tolerance. ');
F = inline('2^x - x^2 - 2');
while abs(F(a) - F(b)) >= tol
c = b - (F(b)*(b-a))/(F(b)-F(a));
if F(a)*F(c) < 0
b = c;
elseif F(b)*F(c) < 0
a = c;
elseif F(c) == 0
break
end
i = i+1;
end
fprintf('%.12f\n', c);

Metro Railway Fare


amount = 0;
N = input('Input value of N. ');
M = input('Input value of M. ');
F = inline('sqrt(1 + ((exp(x)*(50*cos(5*x)) + (1 + 10*sin(5*x))*exp(x)))^2)');
X = linspace(0,6,1000);
area = 0;
for i = 1:length(X)-1
area = area + (X(i+1) - X(i))/2 * (F(X(i)) + F(X(i+1)));
end
total_dist = area * (M-1)/(N-1);
km = total_dist/1000;
if km <= 2
fprintf('Php 7.00\n');
else
fprintf('Php %.2f\n', 7+ceil(km-2));
end

ROOT FINDING
i
a
b
W

=
=
=
=

0;
input('Enter value of a. ');
input('Enter value of b. ');
2.5; I = 30000; E = 50000; L = 600;

tol = input('Enter tolerance. ');


F = inline('(2.5/(120*50000*600*30000))*(-5*(x^4) + 6*(600^2)*(x^2) 600^4)');
y = inline('(2.5/(120*50000*600*30000))*(-(x^5) + 2*(600^2)*(x^3) x*(600^4))');
while abs(F(a) - F(b)) >= tol
c = (a+b)/2;
if F(a)*F(c) < 0
b = c;
elseif F(b)*F(c) < 0
a = c;
elseif F(c) == 0
break
end
i = i+1;
end
fprintf('%.12f\n', c);
disp(y(c));

NUMERICAL INTEGRATION
F = inline('1 - exp(-2*x)');
X = linspace(0,4,1000);
area = 0;
for i = 1:length(X)-1 %Trapezoidal
area = area + (X(i+1) - X(i))/2 * (F(X(i)) + F(X(i+1)));
end
disp(area);
for i = 1:2:length(X)-2 %Simpsons
area = area + (X(i+2) - X(i))/6 * (F(X(i)) + 4*F(X(i+1)) + F(X(i+2)));
end
disp(area);

CALCULUS PROBLEM

N = input('Enter a real number such that 0 < number < 0.5: ');
f = inline('((1/(sqrt(2*pi)))*exp(-(x.^2/2)))');
x_init = 0.5;
x1 = 0;
iter = 10;
ctr = 0;
while ctr ~= iter
area = 0;
x = linspace(0,x,1000);
for z = 2:length(x)
area = area + ((x(z)-x(z-1))/2)*(f(x(z-1))+f(x(z))));
end
x1 = x_init - ((area-N)/f(x_init));
x_init = x1;
ctr = ctr + 1;
end
fprintf('%s.4f', 'The value of x is: ', x1);
fprintf('\n');

Curve Fitting

t = [0.5 1:9]';
y = [7 5.2 3.8 3.2 2.5 2.1 1.8 1.5 1.2 1.1]';
%Model:
%p(t) = Ae^-1.5t + Be^-0.3t + Ce^-0.05t
Z = [exp(-1.5*t) exp(-0.3*t) exp(-0.05*t)];
A = Z'*Z; B = Z'*y;
a = A\B;
disp(a); clf;
scatter(t,y,'b');
yfit = a(1)*exp(-1.5*t) + a(2)*exp(-0.3*t) + a(3)*exp(-0.05*t);
hold on; grid on;
plot(t, yfit, 'r');
____________________________________________________________________
x
y
p
q

=
=
=
=

[24:30]';
[200 310 290 330 360 420 500]';
polyfit(x,y,1); % a0 = -803.214 a1 = 42.5 r2 = 0.9134
polyfit(x,y,2); % a0 = 1181.9 a1 = -105.36 a2 = 2.7381 r2 = 0.9248

Z = [sqrt(x) x];
A = Z'*Z; B = Z'*y;
a = A\B;
disp(a); clf;
scatter(x,y,'b');
yfit = a(1)*sqrt(x) + a(2)*x;
hold on; grid on;
plot(x, yfit, 'r');
ssr = sum((y - yfit).^2);
sst = sum((y - mean(y)).^2);
disp((sst - ssr)/sst);

Newton Rhapson
x = input('Enter initial guess. ');
i = 0;
X = [];
tol = input('Enter tolerance. ');
F = inline('2^x - x^2 -2');
dF = inline('(2^x)*log(2) - 2*x');
while abs(x_init - x) >= tol
x_init = x;
x = x_init - F(x_init)/dF(x_init);
X = [X x_init];
i = i+1;
end
fprintf('%.12f\n', x_init);