Regula–Falsi Method
38 views (last 30 days)
Show older comments
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
0 Comments
Answers (1)
Walter Roberson
on 14 Dec 2020
Edited: Walter Roberson
on 14 Dec 2020
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 Comments
Walter Roberson
on 14 Dec 2020
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?
See Also
Categories
Find more on Get Started with MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!