Add files via upload
This commit is contained in:
+15
@@ -0,0 +1,15 @@
|
||||
Side notes:
|
||||
|
||||
1. This project makes the less use possible of the command window,
|
||||
the purpose would be to create a .exe with a proper GUI, but we soon
|
||||
realized that was too complicated, so this is the closest we could do.
|
||||
for now...
|
||||
|
||||
2. We made this project in English so that we could upload it to github.
|
||||
|
||||
3. Making use of the dialog boxes it became very hard to check for invalid
|
||||
inputs, making use of button options however, overcomes this problem.
|
||||
|
||||
Authors & Mantainers:
|
||||
Ines Ferreira - inesjorge300@gmail.com
|
||||
Andre Neto - netoandre.neto@gmail.com
|
||||
+71
@@ -0,0 +1,71 @@
|
||||
function [c, tab] = bisection (f, x0, x1, tol, max, time)
|
||||
|
||||
% catch errors
|
||||
if (x0 > x1)
|
||||
errordlg('x1 must be bigger than x0','Error')
|
||||
error('x1 must be bigger than x0')
|
||||
end
|
||||
|
||||
if f(x0)*f(x1) > 0
|
||||
errordlg('The interval does not contain a root','Error')
|
||||
error('The interval does not contain a root')
|
||||
end
|
||||
|
||||
% default arguments
|
||||
if nargin < 4
|
||||
tol = 1e-4;
|
||||
max = 20;
|
||||
time = 1;
|
||||
end
|
||||
|
||||
tab = (max);
|
||||
|
||||
x = linspace(x0-1, x1+1, 100);
|
||||
|
||||
figure
|
||||
set(gca,'DataAspectRatio',[1 1 1])
|
||||
plot(x, f(x), "LineWidth", 1)
|
||||
axis([x0-1 x1+1 -4 f(x1+1)])
|
||||
title("Bisection Method")
|
||||
xlabel("𝑥")
|
||||
ylabel("f(𝑥)")
|
||||
text(x0, 0, " 𝑥_0")
|
||||
text(x1, 0, " 𝑥_1")
|
||||
grid on
|
||||
hold on
|
||||
|
||||
shg
|
||||
|
||||
|
||||
|
||||
for i = 1:max
|
||||
|
||||
c = (x0+x1)/2;
|
||||
|
||||
% add element to the table
|
||||
tab(i) = c;
|
||||
|
||||
% animation
|
||||
plot(x0, 0, 'rx', 'MarkerFaceColor', 'r', 'MarkerSize', 8, 'LineWidth',2);
|
||||
plot(x1, 0, 'bx', 'MarkerFaceColor', 'b', 'MarkerSize', 8, 'LineWidth',2);
|
||||
hc = plot(c, 0, 'gx', 'MarkerFaceColor', 'g', 'MarkerSize', 8, 'LineWidth',2);
|
||||
pause(time);
|
||||
|
||||
if f(c) == 0
|
||||
return
|
||||
end
|
||||
|
||||
if (x1-x0)/2 < tol
|
||||
return
|
||||
end
|
||||
|
||||
if f(x1)*f(c) > 0
|
||||
x1 = c;
|
||||
|
||||
else
|
||||
x0 = c;
|
||||
end
|
||||
delete(hc);
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,55 @@
|
||||
function [x1, tab] = newtonr (f, x0, tol, max, time)
|
||||
|
||||
% default arguments
|
||||
if nargin < 3
|
||||
tol = 1e-4;
|
||||
max = 20;
|
||||
time = 1;
|
||||
end
|
||||
|
||||
tab = (max);
|
||||
|
||||
y = sym(f);
|
||||
fp = matlabFunction(diff(y));
|
||||
|
||||
x = linspace(x0-10, x0+10, 1000);
|
||||
|
||||
figure
|
||||
set(gca,'DataAspectRatio',[1 1 1])
|
||||
plot(x, f(x), "LineWidth",2)
|
||||
axis([x0-10 x0+10 -50 50])
|
||||
title("Newton-Raphson Method")
|
||||
xlabel("𝑥")
|
||||
ylabel("f(𝑥)")
|
||||
grid on
|
||||
hold on
|
||||
shg
|
||||
|
||||
for i = 1:max
|
||||
|
||||
% add element to the table
|
||||
tab(i) = x0;
|
||||
|
||||
h1 = plot(x0, f(x0), 'go', 'MarkerFaceColor', 'g', 'MarkerSize', 6, 'LineWidth',2);
|
||||
text(x0, f(x0)+0.5, " 𝑥_{"+ i + "}")
|
||||
pause(time)
|
||||
delete(h1)
|
||||
plot(x0, f(x0), 'bo', 'MarkerFaceColor', 'b', 'MarkerSize', 6, 'LineWidth',2)
|
||||
|
||||
if fp(x0) == 0
|
||||
close all
|
||||
errordlg('x1 is local min/max','Error')
|
||||
return
|
||||
end
|
||||
x1 = x0 - f(x0)/fp(x0);
|
||||
h2 = plot(x, (-f(x0)).*(x-x0)/(x1-x0) + f(x0), '--k', 'LineWidth', 1);
|
||||
pause(time)
|
||||
delete(h2)
|
||||
|
||||
if abs(x1 - x0) < tol
|
||||
return
|
||||
else
|
||||
x0 = x1;
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,84 @@
|
||||
clear, clc, close all;
|
||||
|
||||
% menu interface
|
||||
title = 'Miniproject MCEE 21/21';
|
||||
str = {'Numerical solving of equations', 'Select a Method'};
|
||||
list = {'Bisections', 'Newton-Raphson', 'Secant'};
|
||||
method = listdlg('Name', title, 'PromptString', str, ...
|
||||
'ListSize', [270 45], 'ListString', list, 'SelectionMode', 'single');
|
||||
if isempty(method)
|
||||
return
|
||||
end
|
||||
|
||||
% fetch equation
|
||||
prompt = {'Type an Equation in function of x:'};
|
||||
definput = {'exp(x-3/2)-1'};
|
||||
fun = inputdlg(prompt, title, [1 50], definput);
|
||||
if isempty(fun)
|
||||
return
|
||||
end
|
||||
eq = str2func(['@(x)' vectorize(fun{:})]);
|
||||
|
||||
switch method
|
||||
case 1
|
||||
while true
|
||||
prompt = {'x0:','x1:','tolerance:','maximum iterations:', ...
|
||||
'time between frames (sec)'};
|
||||
definput = {'0', '4', '1e-4', '20', '0.5'};
|
||||
k = inputdlg(prompt, title, [1 50], definput);
|
||||
if isempty(k)
|
||||
return
|
||||
end
|
||||
try
|
||||
[result, tab] = bisection(eq, str2double(k{1}), str2double(k{2}), ...
|
||||
str2double(k{3}), str2double(k{4}), str2double(k{5}));
|
||||
catch ME
|
||||
disp("Error Message: " + ME.message)
|
||||
continue
|
||||
end
|
||||
break
|
||||
end
|
||||
case 2
|
||||
while true
|
||||
prompt = {'x0:','tolerance:','maximum iterations:', ...
|
||||
'time between frames (sec)'};
|
||||
definput = {'0', '1e-4', '20', '0.5'};
|
||||
k = inputdlg(prompt, title, [1 50], definput);
|
||||
if isempty(k)
|
||||
return
|
||||
end
|
||||
try
|
||||
[result, tab] = newtonr(eq, str2double(k{1}), str2double(k{2}), ...
|
||||
str2double(k{3}), str2double(k{4}));
|
||||
catch ME
|
||||
disp("Error Message: " + ME.message)
|
||||
continue
|
||||
end
|
||||
break
|
||||
end
|
||||
case 3
|
||||
while true
|
||||
prompt = {'x0:','x1:','tolerance:','maximum iterations:', ...
|
||||
'time between frames (sec)'};
|
||||
definput = {'0', '4', '1e-4', '20', '0.5'};
|
||||
k = inputdlg(prompt, title, [1 50], definput);
|
||||
if isempty(k)
|
||||
return
|
||||
end
|
||||
try
|
||||
[result, tab] = secant(eq, str2double(k{1}), str2double(k{2}), ...
|
||||
str2double(k{3}), str2double(k{4}), str2double(k{5}));
|
||||
catch ME
|
||||
disp("Error Message: " + ME.message)
|
||||
continue
|
||||
end
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
f = figure;
|
||||
uit = uitable(f);
|
||||
uit.ColumnName = {'Value'};
|
||||
uit.Data = tab(:);
|
||||
|
||||
msgbox("Result = " + result);
|
||||
@@ -0,0 +1,74 @@
|
||||
function [x1, tab] = secant(f, x0, x1, tol, max, time)
|
||||
|
||||
% catch errors
|
||||
if (x0 > x1)
|
||||
errordlg('x1 must be bigger than x0','Error')
|
||||
error('x1 must be bigger than x0')
|
||||
end
|
||||
|
||||
if f(x0)*f(x1) > 0
|
||||
errordlg('The interval does not contain a root','Error')
|
||||
error('The interval does not contain a root')
|
||||
end
|
||||
|
||||
% default arguments
|
||||
if nargin < 4
|
||||
tol = 1e-4;
|
||||
max = 20;
|
||||
time = 1;
|
||||
end
|
||||
|
||||
tab = (max);
|
||||
|
||||
xx = linspace(x0-1, x1+1, 1000);
|
||||
|
||||
figure
|
||||
axis([x0-1 x1+1 -1 inf])
|
||||
set(gca,'DataAspectRatio',[1 1 1])
|
||||
plot(xx, f(xx), 'LineWidth', 1.5)
|
||||
title('Secant Method')
|
||||
xlabel("𝑥")
|
||||
ylabel("f(𝑥)")
|
||||
grid on
|
||||
hold on
|
||||
shg
|
||||
|
||||
% initial markings
|
||||
plot(x0, f(x0), 'ro', 'MarkerFaceColor', 'r', 'MarkerSize', 4)
|
||||
text(x0, f(x0), " f(𝑥_0)")
|
||||
plot(x1, f(x1), 'ro', 'MarkerFaceColor', 'r', 'MarkerSize', 4)
|
||||
text(x1, f(x1), " f(𝑥_1)")
|
||||
|
||||
for i = 1:max
|
||||
|
||||
% calculate x2
|
||||
x2 = (x0*f(x1) - x1*f(x0)) / (f(x1) - f(x0));
|
||||
|
||||
% add element to the table
|
||||
tab(i) = x2;
|
||||
|
||||
% animations
|
||||
line_x0 = plot([x0 x2], [f(x0) 0],'m', 'LineWidth', 1.5, 'LineStyle', '-.');
|
||||
line_x1 = plot([x1 x2], [f(x1) 0],'m', 'LineWidth', 1.5, 'LineStyle', '-.');
|
||||
dot_x2 = plot(x2, 0, 'ro', 'MarkerFaceColor', 'r', 'MarkerSize', 4);
|
||||
pause(time)
|
||||
|
||||
dotted_line = plot([x2 x2],[0 f(x2)],"r", "LineWidth", 1.5, "LineStyle", ":");
|
||||
plot(x2, f(x2), 'ro', 'MarkerFaceColor', 'r', 'MarkerSize', 4)
|
||||
text(x2, f(x2), " f(𝑥_{" + (i + 1) + "})")
|
||||
pause(time)
|
||||
|
||||
% condition
|
||||
if abs(x2-x1)<tol
|
||||
break
|
||||
end
|
||||
|
||||
x0 = x1;
|
||||
x1 = x2;
|
||||
|
||||
delete(line_x0)
|
||||
delete(line_x1)
|
||||
delete(dotted_line)
|
||||
delete(dot_x2)
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user