How do i resolve the error in this code:

% Programme for estimating k in DE
prompt= 'input a value for k : ' ;
k = input(prompt);
w =(pi/12);
n=1:100;
k(n+1)=-1*(log(((12*(((k(n))^2)+(w^2))-(5*k(n))*(k(n)*cos(6*w))+(wsin(6*w)))/(17*((k(n)^2)+(w^2))-5*k(n)*(k(n)*cos(5*w))+(w*sin5w)));
disp (k)
the problem seems to be with the semicolon after the log funtion

2 comentarios

Walter Roberson
Walter Roberson el 10 de Feb. de 2016
You have not indicated what the error is.
Is the user entering a vector or a scalar?
Is k(n) intended to indicate k indexed at n, or k multiplied by n?
Hint: .^ .* ./
reuben crockett
reuben crockett el 10 de Feb. de 2016
a scalar and k indexed at n the error just says missing parenthases

Iniciar sesión para comentar.

 Respuesta aceptada

Star Strider
Star Strider el 10 de Feb. de 2016
Editada: Star Strider el 10 de Feb. de 2016
The problem is most likely that you are missing several operators (probably multiplication operators), as well as vectorising it here:
k(n+1)=-1*(log(((12*(((k(n))^2)+(w^2))-(5*k(n))*(k(n)*cos(6*w))+(wsin(6*w)))/(17*((k(n)^2)+(w^2))-5*k(n)*(k(n)*cos(5*w))+(w*sin5w)));
See if substituting this helps:
k(n+1)=-1*(log(((12*(((k(n))^2)+(w.^2))-(5*k(n))*(k(n)*cos(6*w))+(w.*sin(6*w)))./(17*((k(n)^2)+(w.^2))-5*k(n)*(k(n)*cos(5*w))+(w.*sin(5*w))));
The parentheses don’t match, so you will need to fix that before you run this line. (I don’t know what you’re doing, so I can’t fix them for you.)

2 comentarios

reuben crockett
reuben crockett el 10 de Feb. de 2016
cheers im basically trying to re write an equation into matlab the one found at the top of page 66 of this document
It’s easiest to break the statement out into its constituent parts. That makes it easier to read, and easier to troubleshoot:
N = 12*(k(n)^2 + w.^2) - 5*k(n)*(k(n)*cos(6*w) + w.*sin(6*w));
D = 17*(k(n)^2 + w.^2) - 5*k(n)*(k(n)*cos(5*w) + w.*sin(5*w));
k(n+1) = -log(N./D);
The computer has to do all these calculations and command parsing anyway, so you’re not losing any efficiency by creating the separate variables.
You also need to understand the vectorisation I used in those statements. See the documentation on Array vs. Matrix Operations for all the interesting details.
I ran a version of it to be sure it works. (It does.) I leave the rest to you.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Entering Commands en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 10 de Feb. de 2016

Comentada:

el 10 de Feb. de 2016

Community Treasure Hunt

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

Start Hunting!

Translated by