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

18 次查看(过去 30 天)
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 个评论
Stephen23
Stephen23 2023-11-30
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

请先登录,再进行评论。

回答(1 个)

Fangjun Jiang
Fangjun Jiang 2023-11-30
It seems that you have an extra line of "end". Just delete that line
  2 个评论
Fangjun Jiang
Fangjun Jiang 2023-11-30
编辑:Fangjun Jiang 2023-11-30
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Maintain or Transition figure-Based Apps 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by