How can I properly enter this function in MATLAB

1 view (last 30 days)
Jacob
Jacob on 2 Oct 2022
Moved: DGM on 2 Oct 2022
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.

Answers (2)

Walter Roberson
Walter Roberson on 2 Oct 2022
My guess is
P = p(a+b).*(3-sqrt(3*a+b).*(a+3*b)) ./ (a+b)

DGM
DGM on 2 Oct 2022
Moved: DGM on 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.

Categories

Find more on Programming 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!