How can I properly enter this function in MATLAB

I keep trying to enter an equation in my MATLAB code, and i keep getting array indec errors or mismatched delimeters errors and I was wondering if someone could tell me how I could enter this function: P=p(a+b)(3-sqrt(3*a+b)(a+3*b))/a+b)) in MATLAB.

Réponses (2)

DGM
DGM le 2 Oct 2022
Déplacé(e) : DGM le 2 Oct 2022
Three things:
  1. The expression does have imbalanced parentheses
  2. MATLAB doesn't support implicit multiplication of terms
  3. I don't know the size of these variables
As a consequence of 2 and 3, I don't know whether the expression is supposed to be
P = p(a + b) ... % p is an array and a+b is the index into the array
or
P = p.*(a + b) ... % p is an array (or a scalar) being multiplied by the sum of a and b
Similarly, I can't really know what the intended parentheses is.
You can start by sorting out where the extra parentheses needs to be added/removed. Assuming the remaining operations are elementwise, you need to explicitly multiply the terms. Depending on what's intended, you might wind up with something like this.
P = p.*(a + b).*(3 - sqrt(3*a + b).*(a + 3*b))./a + b;
Bear in mind that's just a guess.

Catégories

En savoir plus sur Operators and Elementary Operations dans Centre d'aide et File Exchange

Question posée :

le 2 Oct 2022

Déplacé(e) :

DGM
le 2 Oct 2022

Community Treasure Hunt

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

Start Hunting!

Translated by