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 Comments

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: .^ .* ./
a scalar and k indexed at n the error just says missing parenthases

Sign in to comment.

 Accepted Answer

Star Strider
Star Strider on 10 Feb 2016
Edited: Star Strider on 10 Feb 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 Comments

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.

Sign in to comment.

More Answers (0)

Categories

Tags

Community Treasure Hunt

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

Start Hunting!