MATLAB Answers

0

unbalanced parenthesis on a problem

Asked by Kylie Nedelka on 1 Feb 2018
Latest activity Edited by John D'Errico
on 1 Feb 2018

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

  3 Comments

The posted link seems to be broken.

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?

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

Sign in to comment.

2 Answers

Answer by John D'Errico
on 1 Feb 2018
Edited by John D'Errico
on 1 Feb 2018

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.

  0 Comments

Sign in to comment.


Answer by Star Strider
on 1 Feb 2018

‘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 ‘f(2)’ refer to? Is ‘f’ a function or a vector?

  0 Comments

Sign in to comment.


Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today