0% found this document useful (0 votes)
13 views3 pages

lab

Uploaded by

rabiabatool0304
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views3 pages

lab

Uploaded by

rabiabatool0304
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

syms x;

y = input('Enter non-linear equation in terms of x: ', 's');


y = str2sym(y);
a = input('Enter first guess: ');
b = input('Enter second guess: ');
e = input('Tolr: ');
fa = double(subs(y, x, a));
fb = double(subs(y, x, b));

if fa * fb > 0
disp('Given initial values do not bracket the root.');

fplot(y, [a-1, b+1]);


hold on;
grid on;
xlabel('x');
ylabel('f(x)');
title('False Position Method - Root Finding');
plot([a b], [fa fb], 'ro');
else
fprintf('\n\na\t\t\tb\t\t\tc\t\t\tf(c)\n');

% Plot the function


fplot(y, [a-1, b+1]);
hold on;
grid on;
xlabel('x');
ylabel('f(x)');
title('False Position Method - Root Finding');
plot([a b], [fa fb], 'ro');

c = a - (a - b) * fa / (fa - fb);
fc = double(subs(y, x, c));

while abs(fc) > e


fprintf('%f\t%f\t%f\t%f\n', a, b, c, fc);
plot(c, fc, 'bo');

if fa * fc < 0
b = c;
fb = double(subs(y, x, b));
else
a = c;
fa = double(subs(y, x, a));
end

c = a - (a - b) * fa / (fa - fb);
fc = double(subs(y, x, c));
end

fprintf('\nRoot is: %f\n', c);


plot(c, fc, 'g*', 'MarkerSize', 10);
legend('f(x)', 'Initial Guesses', 'Iterations', 'Root');
hold off;
end

A = [2 1 -2;
-3 -1 2;
-2 1 2;];
b = [8; -11; -8];
Aug = [A b];
n = size(Aug, 1);

disp('Initial Augmented Matrix:');


disp(Aug);

for i = 1:n

[~, max_index] = max(abs(Aug(i:n, i)));


max_index = max_index + i - 1;
if max_index ~= i
temp = Aug(i, :);
Aug(i, :) = Aug(max_index, :);
Aug(max_index, :) = temp;
disp(['Swapped Row ', num2str(i), ' with Row ', num2str(max_index)]);
disp(Aug);
end

Aug(i, :) = Aug(i, :) / Aug(i, i);


disp(['Normalized Row ', num2str(i)]);
disp(Aug);

for j = i+1:n
Aug(j, :) = Aug(j, :) - Aug(j, i) * Aug(i, :);
disp(['Eliminated Row ', num2str(j), ' using Row ', num2str(i)]);
disp(Aug);
end
end

disp('Upper Triangular Form of Augmented Matrix:');


disp(Aug);

x = zeros(n, 1);
for i = n:-1:1
x(i) = Aug(i, end) - Aug(i, i+1:n) * x(i+1:n);
disp(['Back Substituting for x(', num2str(i), ') = ', num2str(x(i))]);
end

disp('Final Solution:');
disp(x);

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy