How do i resolve the error in this code:
1 回表示 (過去 30 日間)
古いコメントを表示
% 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 件のコメント
Walter Roberson
2016 年 2 月 10 日
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: .^ .* ./
採用された回答
Star Strider
2016 年 2 月 10 日
編集済み: Star Strider
2016 年 2 月 10 日
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 件のコメント
Star Strider
2016 年 2 月 10 日
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 件)
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!