필터 지우기
필터 지우기

Regula–Falsi Method

조회 수: 39 (최근 30일)
Yogesh Bhambhwani
Yogesh Bhambhwani 2020년 12월 14일
댓글: Walter Roberson 2020년 12월 14일
I keep getting an error saying:
Error in solution: Line: 1 Column: 34
Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax error. To construct matrices, use brackets instead of parentheses.
My work is below
function rootArray=RegulaFalsi(f,0.5,1)
f = @(x) x^2-1;
i = 0;
tol = 1e-6;
g = 1;
while(g > tol)
i = i + 1;
c = a - (f(a)*(b-a))/(f(b)-f(a));
if (f(c)*f(a) > 0)
b = c;
g = f(b);
else
a = c;
g = f(a);
end
end
end

답변 (1개)

Walter Roberson
Walter Roberson 2020년 12월 14일
편집: Walter Roberson 2020년 12월 14일
function rootArray=RegulaFalsi(f,0.5,1)
In MATLAB, function must have one of the following syntaxes:
function FunctionName
function FunctionName()
function OutputVariable = FunctionName
function OutputVariable = FunctionName()
function [OutputVariable] = FunctionName
function [OutputVariable] = FunctionName()
function [OutputVariable1, OutputVariable2, ... OutputVariableN] = FunctionName
function [OutputVariable1, OutputVariable2, ... OutputVariableN] = FunctionName()
function [OutputVariable1 OutputVariable2 ... OutputVariableN] = FunctionName
function [OutputVariable1 OutputVariable2 ... OutputVariableN] = FunctionName()
function FunctionName(InputVariable1, InputVariable2, ... InputVariableM)
function OutputVariable = FunctionName(InputVariable1, InputVariable2, ... InputVariableM)
function [OutputVariable] = FunctionName(InputVariable1, InputVariable2, ... InputVariableM)
function [OutputVariable1, OutputVariable2, ... OutputVariableN] = FunctionName(InputVariable1, InputVariable2, ... InputVariableM)
function [OutputVariable1 OutputVariable2 ... OutputVariableN] = FunctionName(InputVariable1, InputVariable2, ... InputVariableM)
Each InputVariable listed may be replaced with ~ such as
function z = sq(~, y)
You will observe that the output variables can be separated by space or comma. The same is not true for the input variables: commas must be used for them.
In each case, where I indicate an input or output variable, a simple variable name must be used, within any () or {} indexing and without any . referencing.
In the input variable list, the ~ mentioned earlier is the only thing permitted instead of a variable name. In particular, constant values such as 0.5 are not permitted.
The function line is a function definition. The names listed in () on the line after the function name are the "dummy" names that will "stand in" for whatever is passed in the corresponding parameter position. In your attempt, whatever was passed in the first position would be known under the name f inside the function. Whatever was passed in the second position... has no way of being associated with any variable inside the function because you did not give a name to the parameter.
Meanwhile, might I point out that inside your function, you use variables a and b before you assign values to them ?
  댓글 수: 4
Yogesh Bhambhwani
Yogesh Bhambhwani 2020년 12월 14일
rootArray is already assigned to: rootArray=RegulaFalsi(f,0.5,1)
Walter Roberson
Walter Roberson 2020년 12월 14일
No, you had the line
function rootArray=RegulaFalsi(f,0.5,1)
as an invalid function definition.
When you corrected that to
function rootArray=RegulaFalsi(f,a,b)
then you do not assign to rootArray inside the function . Any assignment to rootArray outside the function is not going to have an effect on the meaning of the variable inside the function.
You are calculating several variables. Which of the variables do you want to return from the function ? For example it got down to a=0.75 b=0.750001 then what would you want the outcome of the function to be?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by