I'm making a code that will calculate interest on a savings account. I listed all my variables as Double. I compiled it before writing the cout prompts at the bottom, and now that my code is finished, it won't compile. It gives me an error. I tried messing with the amount of parentheses, but nothing helped. Here is my code
FinalBalanceDaily = StartingPrinciple*(pow(1+((SimpInt/100)/365),(365*T)));
FinalBalanceMonthly = StartingPrinciple*(pow(1+((SimpInt/100)/12),(12*T)));
This is the error message.
:55: error: no matching function for call to âpow(double)â
and then it gives notes. The thing is, those two lines are on lines 53 and 54, 55 uses the exp function.
FinalBalanceCont = StartingPrinciple*(exp((SimpInt/100)*T));
EffectiveSimpInt1=(exp(SimpInt*T)-1)/T;
EffectiveSimpInt2=((pow(1+(SimpInt/365),(365*T)))-1)/T;
EffectiveSimpInt3=(pow(1+(SimpInt/12),(12*T))-1)/T;
These are the lines that use the pow() function
std::pow() need two parameters, but you just give one
I suggest that you can make your code more readable:
double x = 1 + (SimpInt / 100) / 12;
double y = 12 * T;
FinalBalanceMonthly = StartingPrinciple * pow(x, y);
By the way, I think that you put your "()" on the wrong place
Related
I am using the following function written in C++, whose purpose is to take the integral of one array of data (y) with respect to another (x)
// Define function to perform numerical integration by the trapezoidal rule
double trapz (double xptr[], double yptr[], int Npoints)
{
// The trapzDiagFile object and associated output file are how I monitor what data the for loop actually sees.
std::ofstream trapzDiagFile;
trapzDiagFile.open("trapzDiagFile.txt",std::ofstream::out | std::ofstream::trunc);
double buffer = 0.0;
for (int n = 0; n < (Npoints - 1); n++)
{
buffer += 0.5 * (yptr[n+1] + yptr[n]) * (xptr[n+1] - xptr[n]);
trapzDiagFile << xptr[n] << "," << yptr[n] << std::endl;
}
trapzDiagFile.close();
return buffer;
}
I validated this function for the simple case where x contains 100 uniformly spaced points from 0 to 1, and y = x^2, and it returned 0.33334, as it should.
But when I use it for a different data set, it returns -3.431, which makes absolutely no sense. If you look in the attached image file, the integral I am referring to is the area under the curve between the dashed vertical lines.
It's definitely a positive number.
Moreover, I used the native trapz command in MATLAB on the same set of numbers and that returned 1.4376.
In addition, I translated the above C++ trapz function into MATLAB, line for line as closely as possible, and again got 1.4376.
I feel like there's something C++ related I'm not seeing here. If it is relevant, I am using minGW-w64.
Apologies for the vagueness of this post. If I knew more about what kind of issue I am seeing, it would be easier to be concise about it.
Plot of the dataset for which the trapz function (my homemade C++ version) returns -3.431:
Please check the value of xptr[Npoints - 1]. It may be less than xptr[Npoints - 2], and was not included in the values that you output.
I'm trying to write an if statement like this
if(denominator([(i-1)! + 1] / i)-1,print(hi),print(ho))
i can be any integer, for example 10. When I set i to 10 it gives this error:
? [(x-1)! + 1] / x
*** this should be an integer: [(x-1)!+1]/x
^-----------
I really only need to check if [(x-1)! + 1] / x is an integer or not. The denominator thing is what I came up with, I also tried Mod but couldn't get that working either.
It seems that you are confused with the names x and i.
Please, see that expression below works properly:
i = 10;
print([(i-1)! + 1] / i);
gp > [362881/10]
I'm not sure what the error was but I ended up using a floor function for determining if it was an integer or not.
You could use:
print(if(((i-1)! + 1) % i, "hi", "ho"))
If i (in your question x) is not an integer, you get an error from the ! (factorial) operator (but see gamma as well).
Do not use [] here, it creates a vector.
The opreator % which I used, gives the remainder. For example 11 % 4 gives the integer 3. In comparison Mod(11, 4) is not an ordinary integer, it is a member of the ring Z/4Z (integers modulo 4). That is very useful in many cases.
I supposed you wanted to write out strings, so I used quotes ". If hi and ho are variables, omit the quotes of course.
I have to calculate the value of arctan(x) . I have calculated the value of this by evaluating the following series :
Arctan (x) = x – x^3/3 + x^5/5 – x^7/7 + x^9/9 - …
But the following code can not calculate the actual value. For example, calculate_angle(1) returns 38.34 . Why?
const double DEGREES_PER_RADIAN = 57.296;
double calculate_angle(double x)
{
int power=5,count=3;
double term,result,prev_res;
prev_res = x;
result= x-pow(x,3)/3;
while(abs(result-prev_res)<1e-10)
{
term = pow(x,power)/power;
if(count%2==0)
term = term*(-1);
prev_res=result;
result=result+term;
++count;
power+=2;
// if(count=99)
// break;
}
return result*DEGREES_PER_RADIAN;
}
EDIT: I found the culprit. You forgot to include stdlib.h, where the function abs resides. You must have ignored the warning about abs being implicitly declared. I checked that removing the include yields the result 38.19 and including it yields the result ~45.
The compiler is not required to stop compilation when an undeclared function is being used (in this case abs). Instead, it is allowed to make assumptions on how the function is declared (in this case, wrong one.)
Besides, like other posters already stated, your use of abs is inappropriate as it returns an int, not a double or float. The condition in the while should be >1e-100 not <1e-100. The 1e-100 is also too small.
--
You forgot to increase count and power after calculating the first two summands:
prev_res = x;
result= x-pow(x,3)/3;
count = 4; <<<<<<
power = 5; <<<<<<
while(abs(result-prev_res)<1e-100)
{
term = pow(x,power)/power;
if(count%2==1)
term = term*(-1);
Also I consider your use of the count variable counterintuitive: it is intialized with 3 like if it denotes the last used power; but then, loop iterations increase it by 1 instead of 2 and you decide the sign by count%2 == 1 as opposed to power%4 == 3
The series converges to tan^{-1} x, but not very fast. Consider the series when x=1:
1 - 1/3 + 1/5 - 1/7 + 1/9 - ...
What is the error when truncating at the 1/9 term? It's around 1/9. To get 10^{-100} accuracy, you would need to have 10^{100} terms. The universe would end before you'd get that. And, catastrophic round-off error and truncation error would make the answer utterly unreliable. You only have 14 digits to play with for doubles.
Look at reference works like Abramowitz and Stegun [AMS 55] or the new NIST Digital Library of Mathematical Functions at http://dlmf.nist.gov to see how these are done in practice. Often, one uses Padé approximants instead of Taylor series. Even when you stick with Taylor series, you often use Chebyshev approximation to cut down on the total error.
I also recommend Numerical Methods that [Usually] Work, by Forman Acton. Or the Numerical Recipes in ... series.
Your sign is the wrong way around after the first two terms. It should be:
if(count%2==0)
term = term*(-1);
Your comparison is the wrong way around in the while condition. Also, you're expecting an unrealistically high level of precision. I would suggest something more like this:
while(fabs(result-prev_res)>1e-8)
Finally, you'll get a more accurate result with a better value for DEGREES_PER_RADIAN. Why not something like this:
const double DEGREES_PER_RADIAN = 180/M_PI;
based on this function. I'm trying to create two empty arrays (one for x and other for y), which later I will use to plot in python. But before anything this is what I have so far...
import math
x1=-2.0
x2=2.0
arr1 = []
arr2 = []
i=0
n=10
delta=(x2-x1)/n
for i in range (0,n+1):
x=x1+delta*i
arr1.append(x)
print arr1
# I have not called the w function yet
the code above creates a list of 10 numbers for now to keep it simple. Then it will send the elements of the array to the function below and compute the equation with certain numbers(infinite loop).
#This function will create the array list for y
import math
def w(x, limit):# the limit here to compare when the number is really small
suma = 0.0
sumb = 0.0
m=1
x=0
suma=suma+((1/(math.pow(2,m))*(math.sin(math.pow(2,m)*x)))
sumb=suma+((1/(math.pow(2,m+1))*(math.sin(math.pow(2,m+1)*x))) # I'm having a
#syntax error
#here
x+=0
if (abs (suma-sumb)<limit):
break:
else m+=1:
if (m<20):
break:
I will appreciate any help with my syntax errors or any suggestion. I just hope I was clear enough.
Thanks ahead of time
The syntax error is actually on the previous line, where the parenthesis are not balanced. You need an extra ) at the end of that line (and at the one you indicated as giving an error too btw).
There are also a few other issues
suma is set to zero, so suma = suma + ... is the same as suma = ..., but I'm guessing you still need to add while loop before this line.
On the line indicated, you have sumb = suma +, which is probably a copy/paste mistake.
The code block starting at x+=0 is indented by only 3 spaces instead of 4. This is probably not the case in your actual code, but if it is, Python will complain about that too.
else m+=1: should be else: m+=1 (colon directly after else, not at the end of the line.
break: should just be break (without to colon).
I have a whole series of assignments which I have put on the same ike using a ";" to seperate the statemnts but I get this error:
1.0; lb(1,9)
1
Error: Unclassifiable statement at (1)
In file LJ.F90:223
I do not understand where there is coming from, when I have the code working if each statement is on its own line. The code is really simple...
What am I stupidly doing wrong.. below code is all on one line.
lb(1,1) = 1.0; lb(1,2) = 1.0; lb(1,3) = 1.0; lb(1,4) = 1.0; lb(1,5) = 1.0; lb(1,6) = 1.0; lb(1,7) = 1.0; lb(1,8) = 1.0; lb(1,9) = 1.0
Your line of code is 134 characters long and, even with Fortran 90-style free format code, most compilers impose a maximum line length. For example, with Sun Studio the default limit is 132 characters.
You can usually increase this character limit using compiler flags, but I suggest splitting that code so that you have one statement per line. It is more legible to human readers and compile- and run-time error messages may be more easily diagnosed.
Adding to the comments of #Deditos, in this case you could use Fortran array notation to reduce the number of lines since all of the elements are being set to the same value:
lb (1, 1:9) = 1.0
Are all elements of the array being initialized to 1.0? Then simply:
lb = 1.0