필터 지우기
필터 지우기

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

조회 수: 18 (최근 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 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