how to get rid of the too many arguments error for "and" while keeping output the same

4 vues (au cours des 30 derniers jours)
y = nthroot(a,3);
a = 0;
b = 2;
xold = a;
for i = 1 - 100
set x = (a + b)/2;
a = 2 + s;
if abs(x-xold) < 10^-8
then % checking required accuracy
FoundSolution = true; % done
Break; % leave for environment
end
if f(a) < 0
then a = a and b = x;
else
a = x and b = b;
end
Not sure how to get rid of the too many arguments error for "and"
  2 commentaires
Les Beckham
Les Beckham le 2 Fév 2023
This code isn't even close to valid Matlab syntax. There are no then or set keywords in Matlab, for example. Also, the normal way to do a logical and of two conditions is with the operator &&. There is a spelled out and but it is a function and is called like this result = and(a,b) where a and b are the conditions like x < 0, for example.
If you are a beginner in using Matlab, I would suggest taking a couple of hours to go through the free tutorial that can be found here: Matlab Onramp
Walter Roberson
Walter Roberson le 2 Fév 2023
set x = (a + b)/2;
In MATLAB, that would look for a function named set and would invoke that function passing in the arguments
'x'
'='
'(a + b)/2'
likewise
then a = a and b = x
would look for a function named then and would call it as
then('a', '=', 'a', 'and', 'b', '=', 'x')
There just happens to be a method named then in toolbox/matlab/connector2/common/+connector/+internal/Future.p

Connectez-vous pour commenter.

Réponses (1)

John D'Errico
John D'Errico le 18 Fév 2023
If you write invalid MATLAB synrtax, then you need to expect your code to fail. Perhaps some of those lines might be valid in other languages, but in MATLAB, expect problems. For example:
for i = 1 - 100
while that line will not overtly fail, it does NOT generate a loop from 1 to 100. Instead, it will set i to the number -99, not even iterating. If you wanted your code to perform a loop from 1 to 100, then write this:
for i = 1:100
Next, we see:
set x = (a + b)/2;
Again, this does not fail directly, because set is an existing MATLAB function. So it tries to use the function set, as a command. But set is not how you assign a value to some variable. Just do this instead:
x = (a + b)/2;
Next we see:
then % checking required accuracy
If statements don't have a then clause. So that line is just a no-op, that does nothing except generate a syntactical problem. The same applies to this line:
then a = a and b = x;
Again, then is meaningless because of the then. I'm not at all sure what you intended with a = a, since that is indeed a no-op, that does noting except toss CPU sybles in the bit bucket. But worse, you also used and. And is not a connector that tells MATLAB to execute two statements on one line.
It looks like you are still writing code in some other language. I won't try to guess which one. But languages have their own syntax. Fail to follow it, or hope that syntax which might be valid for some other language is valid syntax in MATLAB? Then expect to see random garbage for results.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by