Modifying my fprintf code.
显示 更早的评论
Hello,
I have an assignment where the solution is shown and we are assinged to write the code that will duplicate the solution.
So far I have this:
n=9:-1:2;
theta=pi./n;
solution=cos(theta);
fprintf('cos(pi/n) = %7.5f\n',solution)
My problem is I am completely lost as to how to replace the n in my fprintf line with each of the decreasing values of n, so that it shows cos(pi/9), cos(pi/8), etc.
I've been searching MathWorks and using MatLab help to no avail. I have next to no background in coding or math language. (I am not looking for handouts, merely guidance.)
采纳的回答
更多回答(1 个)
Please execute following script.
for n=9:-1:2
theta=pi./n;
solution=cos(theta);
fprintf('cos(pi/%d) = %7.5f\n',n,solution)
end
return value
cos(pi/9) = 0.93969
cos(pi/8) = 0.92388
cos(pi/7) = 0.90097
cos(pi/6) = 0.86603
cos(pi/5) = 0.80902
cos(pi/4) = 0.70711
cos(pi/3) = 0.50000
cos(pi/2) = 0.00000
类别
在 帮助中心 和 File Exchange 中查找有关 Variables 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!