Modifying my fprintf code.

3 views (last 30 days)
Simon Pavlick
Simon Pavlick on 26 Feb 2020
Commented: Les Beckham on 27 Feb 2020
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.)

Accepted Answer

Star Strider
Star Strider on 26 Feb 2020
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 Comments
Star Strider
Star Strider on 27 Feb 2020
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 on 27 Feb 2020
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

Sign in to comment.

More Answers (1)

stozaki
stozaki on 26 Feb 2020
Edited: stozaki on 26 Feb 2020
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 Comment
Simon Pavlick
Simon Pavlick on 26 Feb 2020
Thank you. That (mostly) makes sense.

Sign in to comment.

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!