Modifying my fprintf code.

조회 수: 3 (최근 30일)
Simon Pavlick
Simon Pavlick 2020년 2월 26일
댓글: Les Beckham 2020년 2월 27일
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.)

채택된 답변

Star Strider
Star Strider 2020년 2월 26일
Replace the ‘n’ in ‘pi/n’ with the appropriate format descriptor. I chose ‘%d’ here, then concatenate the ‘n’ vector with the ‘solution’ vector in the fprintf call:
fprintf('cos(pi/%d) = %7.5f\n',[n; solution])
This only addresses part of your assignment (the rest of which you’ve already done), and is difficult to describe or provide hints for.
The full script is now:
n=9:-1:2;
theta=pi./n;
solution=cos(theta);
fprintf('cos(pi/%d) = %7.5f\n',[n; solution])
  댓글 수: 3
Star Strider
Star Strider 2020년 2월 27일
My pleasure!
The upshot of this approach is that no loop is necessary, providing that all the variables to be printed are of the same type. (Variables of different types, such as printing numeric vectors and character vectors, require a loop.) Concatenate the various vectors in a matrix so that each is a row vector in the final matrix, and fprintf (and its friends) will produce the correct result.
Les Beckham
Les Beckham 2020년 2월 27일
I understand that if you have "zero intuition for coding", some of the suggestions given may seem pretty cryptic. The more you read the documentation of whatever programming language you are using, the better you will begin to grasp the common threads in all kinds of programming languages.
Please read the documentation. It has a lot of examples showing how each function works. In this case, look at the documentation for fprintf: https://www.mathworks.com/help/matlab/ref/fprintf.html.
As you read the documentation, try the examples (you can copy/paste them into a test script/function and see how they work when you tweak things). A lot of the docs even have "Try This Example" buttons in the Examples section that allow you to use Matlab Online to change things in the provided example and see the results immediately without creating a local copy and modifying it. One of the great things about Matlab is that it is super easy to change things and try again. Trial and error is one of the best ways to learn.
Probably the most important thing to learn is using the debugger. It allows you to see how things change as you execute each line of your code. Instead of running the entire program and wondering why you didn't get the expected answer at the end, you can see how each step of the process changes things and figure out where things go wrong. Look here as a starting point: https://www.mathworks.com/help/matlab/matlab_prog/debugging-process-and-features.html
I also suggest that all Matlab beginners use the Matlab Onramp tutorial (it is a good intro into the basics of how to use Matlab.): https://www.mathworks.com/learn/tutorials/matlab-onramp.html.
Good luck

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

stozaki
stozaki 2020년 2월 26일
편집: stozaki 2020년 2월 26일
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
  댓글 수: 1
Simon Pavlick
Simon Pavlick 2020년 2월 26일
Thank you. That (mostly) makes sense.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

태그

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by