Azzera filtri
Azzera filtri

Modifying my fprintf code.

3 visualizzazioni (ultimi 30 giorni)
Simon Pavlick
Simon Pavlick il 26 Feb 2020
Commentato: Les Beckham il 27 Feb 2020
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:
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.)

Risposta accettata

Star Strider
Star Strider il 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:
fprintf('cos(pi/%d) = %7.5f\n',[n; solution])
  3 Commenti
Star Strider
Star Strider il 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 il 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:
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:
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.):
Good luck

Accedi per commentare.

Più risposte (1)

stozaki il 26 Feb 2020
Modificato: stozaki il 26 Feb 2020
Please execute following script.
for n=9:-1:2
fprintf('cos(pi/%d) = %7.5f\n',n,solution)
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


Scopri di più su Get Started with MATLAB in Help Center e File Exchange




Community Treasure Hunt

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

Start Hunting!

Translated by