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 个评论

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

请先登录,再进行评论。

 采纳的回答

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 个评论

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.

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Entering Commands 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by