Regula–Falsi Method

37 vues (au cours des 30 derniers jours)
Yogesh Bhambhwani
Yogesh Bhambhwani le 14 Déc 2020
Commenté : Walter Roberson le 14 Déc 2020
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

Réponses (1)

Walter Roberson
Walter Roberson le 14 Déc 2020
Modifié(e) : Walter Roberson le 14 Déc 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 commentaires
Yogesh Bhambhwani
Yogesh Bhambhwani le 14 Déc 2020
rootArray is already assigned to: rootArray=RegulaFalsi(f,0.5,1)
Walter Roberson
Walter Roberson le 14 Déc 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?

Connectez-vous pour commenter.

Catégories

En savoir plus sur Get Started with MATLAB dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by