I am getting a syntax error with +

I need to calculate the value of y for a hw problem using an element by element operation but the first + is giving me a syntax error and I have no idea what I did wrong. (sorry for the abundance of parenthesis, this is the only way I could figure it out). Where I have the first (t.+1) the + is giving me an error
t=[0,1,2,3,4,5,6,7,8];
y=((20.*t.^(2/3))/(t.+1))-(((t.+1)^2)/(e^((.3.*t)+5)))+(2/(t.+1))

 Respuesta aceptada

Sean de Wolski
Sean de Wolski el 20 de Sept. de 2017
t=[0,1,2,3,4,5,6,7,8];
y=((20.*t.^(2/3))/(t+1))-(((t+1).^2)/(exp((.3.*t)+5)))+(2./(t+1))
+ is implied to be an elementwise operation so you do not need a . in front of it. You do need one in front of the / and ^. There is a convenience function vectorize that will do this for you.
Also note, exp() instead of e^

3 comentarios

Kimberly Siwek
Kimberly Siwek el 20 de Sept. de 2017
Editada: Walter Roberson el 20 de Sept. de 2017
Thank you, that fixed the + and thanks for the exp(), the professor hasn't gone over it yet. It's working now, but just out of wanting to understand what's happening at y= the = is giving me a red squiggle and when I hover right before it, it tells me 1x9 double with each value in the column listed and if I put the mouse directly after it tells me to terminate statement with semicolon to suppress output. is it the program just telling me what the results will be without running it yet and the option that I have with that line of code? Thanks
t=[0,1,2,3,4,5,6,7,8];
y=((20.*t.^(2/3))./(t+1))-(((t+1).^2)./(exp((.3.*t)+5)))+(2./(t+1))
Walter Roberson
Walter Roberson el 20 de Sept. de 2017
That is a warning pointing out that you do not have a semi-colon after the expression, and because of that the result of the calculation will be automatically displayed. If you do not want the result to be automatically displayed then add a semi-colon at the end of the line.
Sean de Wolski
Sean de Wolski el 21 de Sept. de 2017
And to be pedantic, it's probably an orange squiggle not a red one :)
The red means it's broken and will not run; orange is for warnings, best practices, possible bugs, etc.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre MATLAB Coder en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 19 de Sept. de 2017

Comentada:

el 21 de Sept. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by