Asked by Kylie Nedelka
on 1 Feb 2018 at 16:15

trying to write this on matlab and keep gettting error in parenthesis

Answer by John D'Errico
on 1 Feb 2018 at 16:52

Edited by John D'Errico
on 1 Feb 2018 at 20:11

I would start by writing it using valid syntax for MATLAB. This is not that.

2cos(2pi((f(1)-f(2)/2)t)sin(2pi((f(1)+f(2)/2)t)

If you do not put a * between 2 and cos, then MATLAB will generate an error. The same applies for all of the other spots where you did not use * for multiplication. That it is common shorthand to not use a multiplication operator when you write, that does not apply to MATLAB. If you choose not to use *, you will get all sorts of errors, but not valid code.

MATLAB uses * for multiplication. Use it.

As far as the valid use os parens, I cannot even know for certain what you intended to write. There are (at least) two missing right parens in that expression.

sum('2cos(2pi((f(1)-f(2)/2)t)sin(2pi((f(1)+f(2)/2)t)' == '(') ans = 10

sum('2cos(2pi((f(1)-f(2)/2)t)sin(2pi((f(1)+f(2)/2)t)' == ')') ans = 8

Edit:

Now that you have actually attached a valid picture of what you want, write it in pieces. If you cannot figure out how to write an expression, just start in the inside.

f(1) - f(2)

Then divide by 2. Note that at each and every step, I'm making sure the parens matchup, because I added at most one left and one right parens at a time.

(f(1) - f(2))/2

Multiply by 2*pi.

2*pi*(f(1) - f(2))/2

and by t...

2*pi*(f(1) - f(2))/2*t

Here, make sure you understand that division and multiplication have the same order of precedence. So in MATLAB, we understand that 2/2*2 is 2, because MATLAB parses that as essentially (2/2)*2. Be careful, because many people make the mistake, thinking that 2/2*2==2/(2*2).That is not true.

Now take the cosine...

cos(2*pi*(f(1) - f(2))/2*t)

Do the same for the second half of the expression, which is identical, except for sin versus cos, and a plus in the middle. You should get this:

sin(2*pi*(f(1) + f(2))/2*t)

Combine the two sub-expressions, and multiply by 2

2*cos(2*pi*(f(1) - f(2))/2*t) * sin(2*pi*(f(1) + f(2))/2*t)

All I did was work outwards, in very simple steps.

If you can't solve a problem that is too large for you to handle, break it down into SMALL pieces, all of which are solvable. And learn to use MATLAB operators.

Log in to comment.

Answer by Star Strider
on 1 Feb 2018 at 16:51

*‘sorry, it is supposed to be 2cos(2pi((f(1)-f(2)/2)t)sin(2pi((f(1)+f(2)/2)t)’*

MATLAB does not recognise implied multiplication. You need to insert the appropriate operators.

*Example* —

2*cos(2*pi*((f(1)-f(2)/2)*t)*sin(2*pi((f(1)+f(2)/2)*t)

Unless you intend to do array operations, use **Vectorization** (link) to do element-wise operations.

Also, what do ** ‘f(1)’** and

Log in to comment.

Opportunities for recent engineering grads.

Apply Today
## 3 Comments

## M (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/380221-unbalanced-parenthesis-on-a-problem#comment_530942

The posted link seems to be broken.

## Rik Wisselink (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/380221-unbalanced-parenthesis-on-a-problem#comment_530949

Here is a correct link.

I don't know what that bar at the end is supposed to mean, but none of this is Matlab yet. What did you try?

## Kylie Nedelka (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/380221-unbalanced-parenthesis-on-a-problem#comment_530955

sorry, it is supposed to be 2cos(2pi((f(1)-f(2)/2)t)sin(2pi((f(1)+f(2)/2)t) and i cannot seem to type it correctly in matlab

Log in to comment.