commit 28d03b01898061d5fcce2d8ea9f7d4b9f6076587 Author: agsNeto20 <37492761+agsNeto20@users.noreply.github.com> Date: Mon May 30 15:25:03 2022 +0200 Add files via upload diff --git a/README.txt b/README.txt new file mode 100644 index 0000000..ab46b29 --- /dev/null +++ b/README.txt @@ -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 diff --git a/bisection.m b/bisection.m new file mode 100644 index 0000000..40fa6ba --- /dev/null +++ b/bisection.m @@ -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 diff --git a/newtonr.m b/newtonr.m new file mode 100644 index 0000000..a4dab2f --- /dev/null +++ b/newtonr.m @@ -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 \ No newline at end of file diff --git a/projetodlg.m b/projetodlg.m new file mode 100644 index 0000000..f92479e --- /dev/null +++ b/projetodlg.m @@ -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); diff --git a/secant.m b/secant.m new file mode 100644 index 0000000..44e0741 --- /dev/null +++ b/secant.m @@ -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)