Beruflich Dokumente
Kultur Dokumente
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);
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;
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);