Borrar filtros
Borrar filtros

how can I fix this error "Illegal use of reserved keyword "end"."

18 visualizaciones (últimos 30 días)
Anne
Anne el 30 de Nov. de 2023
Comentada: Stephen23 el 30 de Nov. de 2023
function SolveButtonPushed(app, event)
% Get the function, interval endpoints, and tolerance from the input fields
func = str2func(['@(x)' app.FunctionEditField.Value]);
a = app.LeftEndpointEditField.Value;
b = app.RightEndpointEditField.Value;
tol = app.ToleranceEditField.Value;
% Perform the bisection method
[root, iter] = bisectionMethod(func, a, b, tol, 1000);
% Display the result in the text area
app.ResultTextArea.Value = sprintf('Root: %f\nIterations: %d', root, iter);
end
end
% App creation and deletion
methods (Access = public)
% Construct the app
function app = BisectionMethodApp
% Create and configure components
app.UIFigure = uifigure;
app.FunctionLabel = uilabel(app.UIFigure, 'Text', 'Enter the function:');
app.FunctionEditField = uieditfield(app.UIFigure, 'Text', '', 'Position', [10, 100, 100, 22]);
app.LeftEndpointLabel = uilabel(app.UIFigure, 'Text', 'Left Endpoint:');
app.LeftEndpointEditField = uieditfield(app.UIFigure, 'numeric', 'Position', [10, 70, 100, 22]);
app.RightEndpointLabel = uilabel(app.UIFigure, 'Text', 'Right Endpoint:');
app.RightEndpointEditField = uieditfield(app.UIFigure, 'numeric', 'Position', [10, 40, 100, 22]);
app.ToleranceLabel = uilabel(app.UIFigure, 'Text', 'Tolerance:');
app.ToleranceEditField = uieditfield(app.UIFigure, 'numeric', 'Position', [10, 10, 100, 22]);
app.SolveButton = uibutton(app.UIFigure, 'Text', 'Solve', 'Position', [10, 10, 100, 22], 'ButtonPushedFcn', @app.solveButtonPushed);
app.ResultTextArea = uitextarea(app.UIFigure, 'Position', [10, 10, 100, 22]);
app.UIFigure.Visible = 'on';
end
end
end
function [root, iterations] = bisectionMethod(func, a, b, tol, maxIterations)
% Check if the function has different signs at the interval endpoints
if func(a) * func(b) >= 0
error('Function has the same sign at the interval endpoints. Bisection method cannot be applied.');
end
% Initialize variables
iterations = 0;
root = (a + b) / 2;
% Perform the bisection method
while [(b - a) / 2 > tol && iterations < maxIterations]
root = (a + b) / 2;
if func(root) == 0
break;
elseif func(a) * func(root) < 0
b = root;
else
a = root;
end
iterations = iterations + 1;
end
end
  1 comentario
Stephen23
Stephen23 el 30 de Nov. de 2023
Badly aligned code is buggy and hides bugs. Your code is badly aligned, is buggy, and hides those bugs.
Solution: align your code correctly (hint: select all code, press ctrl+i). When you align your code consistently many superflous ENDs are very obvious simply by looking (I count four of them):
function SolveButtonPushed(app, event)
% Get the function, interval endpoints, and tolerance from the input fields
func = str2func(['@(x)' app.FunctionEditField.Value]);
a = app.LeftEndpointEditField.Value;
b = app.RightEndpointEditField.Value;
tol = app.ToleranceEditField.Value;
% Perform the bisection method
[root, iter] = bisectionMethod(func, a, b, tol, 1000);
% Display the result in the text area
app.ResultTextArea.Value = sprintf('Root: %f\nIterations: %d', root, iter);
end
end
% App creation and deletion
methods (Access = public)
% Construct the app
function app = BisectionMethodApp
% Create and configure components
app.UIFigure = uifigure;
app.FunctionLabel = uilabel(app.UIFigure, 'Text', 'Enter the function:');
app.FunctionEditField = uieditfield(app.UIFigure, 'Text', '', 'Position', [10, 100, 100, 22]);
app.LeftEndpointLabel = uilabel(app.UIFigure, 'Text', 'Left Endpoint:');
app.LeftEndpointEditField = uieditfield(app.UIFigure, 'numeric', 'Position', [10, 70, 100, 22]);
app.RightEndpointLabel = uilabel(app.UIFigure, 'Text', 'Right Endpoint:');
app.RightEndpointEditField = uieditfield(app.UIFigure, 'numeric', 'Position', [10, 40, 100, 22]);
app.ToleranceLabel = uilabel(app.UIFigure, 'Text', 'Tolerance:');
app.ToleranceEditField = uieditfield(app.UIFigure, 'numeric', 'Position', [10, 10, 100, 22]);
app.SolveButton = uibutton(app.UIFigure, 'Text', 'Solve', 'Position', [10, 10, 100, 22], 'ButtonPushedFcn', @app.solveButtonPushed);
app.ResultTextArea = uitextarea(app.UIFigure, 'Position', [10, 10, 100, 22]);
app.UIFigure.Visible = 'on';
end
end
end
function [root, iterations] = bisectionMethod(func, a, b, tol, maxIterations)
% Check if the function has different signs at the interval endpoints
if func(a) * func(b) >= 0
error('Function has the same sign at the interval endpoints. Bisection method cannot be applied.');
end
% Initialize variables
iterations = 0;
root = (a + b) / 2;
% Perform the bisection method
while [(b - a) / 2 > tol && iterations < maxIterations]
root = (a + b) / 2;
if func(root) == 0
break;
elseif func(a) * func(root) < 0
b = root;
else
a = root;
end
iterations = iterations + 1;
end
end

Iniciar sesión para comentar.

Respuestas (1)

Fangjun Jiang
Fangjun Jiang el 30 de Nov. de 2023
It seems that you have an extra line of "end". Just delete that line
  2 comentarios
Anne
Anne el 30 de Nov. de 2023
i deleted it but matlab still report the same error
Fangjun Jiang
Fangjun Jiang el 30 de Nov. de 2023
Editada: Fangjun Jiang el 30 de Nov. de 2023
You have 4 "end" in a roll from line 80 to 85. Delete line 85 too. Notice the vertical bar on line 81 between nuber "81" and "end"? It indicates that this "end" is paired with some other key words like "for", "if", etc.
The "end" on Line 84 and 85 doesn't have that bar. It indicates they are un-necessary.

Iniciar sesión para comentar.

Categorías

Más información sobre Maintain or Transition figure-Based Apps en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by