Hi all. I've been working on this problem for a week but just can't crack it. I'm very new to c++ and taking a class on it, and this is one of the questions. I'm not that strong at plotting graphs yet, and I received some feedback that I should put the outputs in array first, but I suuuck at arrays. =/
The output should be a parabolic line with x values from 0-1 and y values from 0-0.1 in the downward direction. (but not negative values)
The attached code I have is whack. My x values are increasing to 1 at a weird increment, then decreasing back to 0, and my plots are linear, not parabolic.
Any help would be greatly appreciated. If you simply post the workable code for the problem, please, if you could, explain the plotting and the array if you use one.
Maybe you should start by stating your equation correctly.
Your code doesn't correspond to your stated equation, and neither of them are parabolic (quadratic) in u. (So it's a silly question).
Your coded equation is at least that given in Chegg.
Dump your output here to file and plot it in gnuplot. C++ doesn't do native plots: try it in Python and use matplotlib if you want that.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include<iostream>
#include<iomanip>
usingnamespace std;
int main()
{
constdouble k = 0.25;
constint N = 20;
constdouble du = 0.05;
for ( int i = 0; i <= N; i++ )
{
double u = i * du;
double P = k * u * ( 1 - u ) / ( k + u );
cout << fixed << setprecision(2) << setw(10) << u << setprecision(4) << setw(10) << P << '\n';
}
}
Thanks for the help. I can get the data plots no problem. Bu the output specifically needs to be the plotted graph. We've used for statements in the past to plot. So the graph needs to be in c++. That's the main thing I'm having trouble with.
Standard C++ doesn't have graphics.
If you want to draw from c++ you need a plotting library. Which plotting library do you have available/want to use?
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
usingnamespace std;
int main()
{
constdouble k = 0.25;
constint N = 20;
constdouble du = 0.05;
vector<string> canvas( 21, string( 1+N, ' ' ) );
for ( int i = 0; i <= N; i++ )
{
double u = i * du;
double P = k * u * ( 1 - u ) / ( k + u );
// cout << fixed << setprecision(2) << setw(10) << u << setprecision(4) << setw(10) << P << '\n';
canvas[(int)(200*P+0.5)][i] = '*';
}
for ( int i = 0; i <= 20; i++ ) cout << canvas[i] << '\n';
}