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

27 ビュー (過去 30 日間)
Anne
Anne 2023 年 11 月 30 日
コメント済み: Stephen23 2023 年 11 月 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 件のコメント
Anne
Anne 2023 年 11 月 30 日
i deleted it but matlab still report the same error
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 Center および File ExchangeCharacters and Strings についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by