I'm new to this whole programming things so bear with me.
I want to make a program that could calculat the area between quadratic and x-axis.
Right now my code is only designed for functions were a is postive and c is negative.
#include <stdio.h>
int main()
{
float a;
float b;
float c;
float x; /* this is the zero that is to the right*/
float y; /* this is the zero that is to the left*/
float z;
{
printf("Consider the function ax^2 + bx + c\n");
printf("Enter the a value: \n");
scanf("%f",&a);
printf("Enter the b value: \n");
scanf("%f",&b);
printf("Enter the c value: \n");
scanf("%f", &c);
x = (( -b + sqrt(b*b - 4*a*c)) / (2*a));
y = (( -b - sqrt(b*b - 4*a*c)) / (2*a));
do {
z=(((y+0.01)-(y))*((a*(y*y))+(b*y)+(c)));
y+0.01;} while (x>y);
if (y>=x) {
printf("The area is %f", z);
}
The problem is that the program just never stops running. What im trying to do is to make small squares and measure their area (remmember LRAM and RRAM). So what im doing is (zero + a little bit) times y value (a*(y*y))+(b*y)+(c)))`
Any tips?
In
do {
z=(((y+0.01)-(y))*((a*(y*y))+(b*y)+(c)));
y+0.01;
} while (x>y);
you should change y+0.01 to y += 0.01, if you use y+0.01, y never change during the loop.
Increment both z and y in the do while loop.
z = 0; //initialize z to remove any garbage values.
do {
// here z is NOT getting assigned, but incremented each time this loop runs.
// NOTE: A += C; is short for A = A + C;
z += (((y+0.01)-(y))*((a*(y*y))+(b*y)+(c)));
y += 0.01;
}
while (x>y);
I understand it doesn't help immediately, but for stuff like this, Numerical Recipes is the ultimate book. The old C version (which of course, still works quite nicely in C++) is available for free. They also have a C++ version you can buy.
It has code for every algorithm in the book, and explains step by step what you are doing and why.
Link to homepage
Link to C version
Related
We are doing C Programming in school and we have to calculate the cosine of a number with use of the "Taylor series" (https://en.wikipedia.org/wiki/Taylor_series). I know C programming but that what we have to do has not really much to do with programming itself more than being good in math.
If you put in your calculator cos(50) it's 0.6427.... That's what we have to do in C. There is the cos() function in math.h but that's not actually the cosine. I am overwhelmed with this and don't really know what we have to do. It should look something like this:
#include <stdio.h>
#include <conio.h>
#include <math.h>
int fac(int n);
double meincosinus(double x);
void main() {
double x;
double y;
int i;
printf("geben sie eine zahl ein\n");
scanf_s("%lf", &x);
y = meincosinus(x);
printf_s("cos(%lf)=%lf\n", x, y);
y = sin(x);
printf_s("cos(%lf)=%lf\n", x, y);
scanf_s("%d", &i);
printf_s("%d\n", i);
_getch();
}
int fac(int n) {
int prod = 1;
int i;
for (i = 1; i <= n; i++)
{
prod *= i;
}
return prod;
}
double meincosinus(double x)
{
double erg = 0;
int n;
for (n = 0; n <= x; n++)
{
x = fac(n);
erg += pow(-1, n) * pow(x, (2 * n)) / fac(2 * n);
}
return erg;
}
This code runs but the output is wrong.
Not an answer (but, well, I am a teacher myself, I am not gonna make your homework :)). But a few questions you should ask yourself and that might help you
Why are you comparing your Taylor computation of cosinus with the "real" sinus? Shouldn't y=sin(x) be y=cos(x), for a pertinent comparison?
why are you doing this: x = fac(n);? You are overwriting your x argument to "meinconsinus". You can't expect meincosinus(50) to really compute cos(50) if the first thing you do is overwriting that "50" with something else (namely n!).
also, why this for (n = 0; n <= x; n++). You know the Taylor formula better that I do (since you are studying it right now). It sure not supposed to stop after x iterations. x is rarely even an integer. It is supposed to be very small anyway. And could even be negative. So, question you've to ask yourself is, how many iterations (up to which term of the series) you want to compute. It is a rather arbitrary choice, since from Taylor point of view, answer is ∞. But on a computer, after a while it is no use to add extremely small numbers
Since I am mentioning the fact that x is supposed to be small: you can't compute cos(50) that way with a Taylor formula. You could compute cos(0.1) or even cos(1) maybe, with enough iterations. But 50 is not a small enough number. Plus, 50ⁿ will be very quickly out of control in your loop. If you really want meincosinus to be able to handle any number, you have first to reduce x, using trigonometric rules: cos(x)=cos(x)-2π; cos(x)=-cos(x); cos(x)=sin(π/2-x); ... There are some better rules, but with those simple ones, you can have x in [0,π/4]. Since π/4<1, at least you don't have an explosive xⁿ.
Also, but that is an optimization, you don't really need to compute neither (-1)ⁿ with pow (just alternate a int sign variable, between -1 and 1 at each iteration), nor x²ⁿ (just multiply a double x2n=1 by x*x each iteration), nor fac(2n) (just multiply a f=1 variable by (n-1)×n, being careful with 0 case, at each iteration.
This is my homework:
I haven't tried to write the part of Natural Logarithm because I can't solve the part of Exponential.
This is the the approximations of Exponential in C using Taylor Series expansion I wrote.
However, it returns inf. What did I do wrong?
#include <stdio.h>
// Returns approximate value of e^x
// using sum of first n terms of Taylor Series
float exponential(int n, float x)
{
float sum = 1.0f; // initialize sum of series
for (int a = n; a >= 0; ++a ) {
while (x * sum / a < 0.00001) {
break;
}
sum = 1 + x * sum / a;
return sum;
}
}
int main()
{
int n = 0;
float x = 1.0f;
printf("e^x = %.5f", exponential(n, x));
return 0;
}
With How do I ask and answer homework questions? in mind, I will give you a few things to have a careful look at.
From comment by Spektre:
from a quick look you are dividing by zero in while (x * sum / a < 0.00001) during first iteration of for loop as a=n and you called the function with n=0 ... also your code does not match the expansion for e^x at all
Have a look at the for loop:
for (int a = n; a >= 0; ++a )
What is the first value of a? The second? The third?
Keep in mind that the values are determined by ++a.
When will that loop end? It is determined by a >= 0. When is that false?
What is this loop doing?
while (x * sum / a < 0.00001) {
break;
}
I suspect that you programmed "English to C", as "do the outer loop while ...", which is practically from the assignment.
But the loop does something else. Apart from risking the division by 0 mentioned above, if the condition is true it will stay true and cause an endless loop, which then however is immediatly canceled in the first iteration.
The head of your function float exponential(int n, float x) expects n as a parameter. In main you init it with 0. I suspect you are unclear about where that value n is supposed to come from. In fact it is unknown. It is more a result of the calculation than an input.
You are supposed to add up until something happens.
You do not actually ever need the value of n. This means that your for loop is meaningless. The inner loop (though currently pointless) is much closer to your goal.
I will leave it at this for now. Try to use this input.
Feel free to edit the code in your question with improvements.
(Normally that is not appreciated, but in case of homework dialog questions I am fine with it.)
Your current implementation attempt is quite a bit off. Therefore I will describe how you should approach calculating such a series as given in your quesiton.
Let's look at your first formula:
You need to sum up terms e(n) = x^n / n!
To check with your series: 1 == x^0 / 0! - x == x^1 / 1! - ...
To calculate these terms, you need a simple rule how to get from e(n) to e(n+1). Looking at the formula above we see that you can use this rule:
e(n+1) = e(n) * x / (n+1)
Then you need to create a loop around that and sum up all the bits & pieces.
You are clearly not supposed to calculate x^n/n! from scratch in each iteration.
Your condition to stop the loop is when you reach the limit of 1e-5. The limit is for the new e(n+1), not for the sum.
For the other formulas you can use the same approach to find a rule how to calculate the single terms.
You might need to multiply the value by -1 in each step or do something like *x*n/(n+1) instead of *x/(n+1) etc.
Maybe you need to add some check if the formula is supposed to converge. Then maybe print some error message. This part is not clear in your question.
As this is homework, I only point into the direction and leave the implementation work to you.
If you have problems with implementation, I suggest to create a new question.
#include <stdio.h>
int main() {
float power;
printf("Enter the power of e\n");
scanf("%f", &power);
float ans = 1;
float temp = 1;
int i = 1;
while ((temp * power) / i >= 0.00001) {
temp = (temp * power) / i;
ans = ans + temp;
i++;
}
printf("%.5f", ans);
return 0;
}
I think I solved the problem
But the part about Natural Log is not solved, I will try.
#include <stdio.h>
#include <math.h>
int main(void)
{
double r,d,x,y,pi;
clrscr();
printf("Input the value of r and degree: ");
//scanf("%f",&r);
//scanf("%f",&d);
r = 12;
d = 195;
pi = 3.14;
x = r * cos(d * pi/180);
y = r * sin(d * pi/180);
printf("The polar form is: (%f,%f)", x, y);
getch();
}
In the 1st case with defined values of r and d the output comes correct but in 2nd case when I give the input the output doesn't match with the original answer. The code worked on codeblocks but isn't working on turbo c++.
what am I doing wrong in turbo c++?
Mis-matched format specifier. Use "%lf" with a double *
double r,d,x,y,pi;
...
//scanf("%f",&r);
//scanf("%f",&d);
scanf("%lf",&r);
scanf("%lf",&d);
Better codes checks for success before using r.
if (scanf("%lf",&r) != 1) Handle_Error();
Output is Cartesian coordinates. #Jens. I'd also recommend a '\n'.
// printf("The polar form is: (%f,%f)", x, y);
printf("The Cartesian form is: (%f,%f)\n", x, y);
It appears Turbo-C requires "l" when printing double, so use
printf("The Cartesian form is: (%lf,%lf)\n", x, y);
Little reason to use a low precision pi. Suggest
pi = acos(-1.0);
In:
//scanf("%f",r);
//scanf("%f",d);
You need to pass the addresses of the variables, &r and &d.
Check the image
This is my 1st post so have that in mind while reading my question.
I have an exam of a colloquium but my code does not provide me the correct result.
So if anyone could help me that would be great. :)
These are the informations that are provided in the exam:
A function y=f(x)=ax^2+bx+c
We have to find the surface that is below the chart but keep in mind that dx(Delta X)=B-A and the height goes like this: A,A+dx,A+2dx, .... , B-dx.
As dx value gets lower the surface will be more accurate.
You have to write the program so that the surface with precision 0.001
This is my code so could someone who is good in C check it please.
Thank you.
#include <stdio.h>
#include <math.h>
int main()
{
int a,b,c;
double A,B,dx,p,D,q,x,y,nv,nv1,nv2,sv;
do{
printf("Insert a & b: "),scanf("%lf %lf",&A,&B);
} while(A<1 || B<1);
nv=dx=B-A;
do{
printf("enter odds: "),scanf("%d %d %d",&a,&b,&c);
p=(-b)/2;
D=sqrt(pow(b,2)-4*a*c);
q= -D/4*a;
} while( a<0 || p<0 || q<0);
do{
sv=nv;
dx/=2;
nv=0;
for(x=A;x<p;x+=dx)
for(dx=B-A;dx<q;dx/=2)
nv1+=x*dx;
for(y=p;y<=B;y+=dx)
for(dx=q;dx<B;dx/=2)
nv2+=y*dx;
nv=nv1+nv2;
}while(fabs(nv-sv)>0.001);
printf("The surface is %lf",nv);
return 0;
}
You want to find the approximation of a definite integral of a quadratic function. There are several issues with your code:
What is the restriction of A ≥ 1 and B ≥ 1 for? A parabola is defined over the whole abscissa. If anything, you should enforce that the input is numeric and that two values were given.
You don't need to find the vertex of the parabola. Your task is to create small rectangles based on the left x value of each interval as the image shows. Therefore, you don't need p and q. And you shouldn't enforce that the vertex is in the first quadrant on the input without indication.
Why are the coefficients of the parabola integers? Make them doubles to be consistent.
Because you don't need to know the vertex, you don't need to split your loop in two. In your code, you don't even check that p is between A and B, which is a requirement of cour code.
What is the inner loop for? You are supposed to just calculate the area of the current rectangle here. What's worse: you re-use the variable dx as iteration variable, which means you lose it as an indicator of how large your current interval is.
The repeated incrementing of dx may lead to an accumulated floating-point error when the number of intervals is large. A common technique to avoid this is to use an integer variable for loop control and the determine the actual floating-point variable by multiplication.
The absolute value as a convergence criterion may lead to problems with small and big numbers. The iteration ends too early for small values and it may never reach the criterion for big numbers, where a difference of 0.001 cannot be resolved.
Here's a version of your code that puts all that into practice:
#include <stdio.h>
#include <math.h>
int main()
{
double a, b, c;
double A, B;
printf("Lower and upper limit A, B: ");
scanf("%lf %lf", &A, &B);
printf("enter coefficients a, b, c: ");
scanf("%lf %lf %lf", &a, &b, &c);
double nv = 0;
double sv;
int n = 1;
do {
int i;
double dx;
sv = nv;
n *= 2;
dx = (B - A) / n;
nv = 0;
for (i = 0; i < n; i++) {
double x = A + i * (B - A) / n;
double y = a*x*x + b*x + c;
nv += dx * y;
}
} while(fabs(nv - sv) > 0.0005 * fabs(nv + sv));
printf("Surface: %lf\n", nv);
return 0;
}
The code is well-behaved for empty intervals (where A = B) or reversed intervals (where A > B). The inpt is still quick and dirty. It should really heck that the entered values are valid numbers. There's no need to restrict the input arbitrarily, though.
I'm new to the C language and am trying to do a lab tutorial that we were given at uni.
We've been asked to do the following:
Task 1.
The Babylonian algorithm to compute the square root of a number n is as follows:
1. Make a guess at the answer (you can pick n/2 as your initial guess).
Compute r = n / guess
Set guess = (guess +r) / 2
Go back to step 2 for as many iterations as necessary. The more that steps 2 and 3 are
repeated, the closer guess will become to the square root of n.
Write a program that inputs an integer for n, iterates through the Babylonian algorithm
five times, and outputs the answer as a double to two decimal places. Your answer will
be most accurate for small values of n.
Here is what I have written:
#include <stdio.h>
#include <math.h>
int n;
main(void){
printf("Enter a value for n: ");
scanf("%d",&n);
double guess = n / 2;
for (int i = 0; i < 5; i++) {
double r = n / guess;
double guess = (guess + r) / 2;
}
printf("%d",guess);
}
Where have I gone wrong? It spits out ridiculous results; for example if I input "4" as n, the answer should be around "2", but it gives different huge results each time.
Another solution would be:
guess = guess / 2.0;
This would "force" a floating-point operation.
And the variable guess is already in the scope. You can´t redeclare it (as you did inside the loop). You can only set it a new value.
And you also need to change the printf to :
printf("%f",guess);
Check this link for more info about the printf formatters:
http://www.cplusplus.com/reference/cstdio/printf/
A few things wrong here.
First, you have scoped a second instance of guess inside the loop. Take away the double declaration on that line. So it should become:
guess = (guess + r) / 2;
Second, because guess is a double you need to use %f instead of %d in the printf call.
printf( "%f", guess );
Once you get it working, consider running the algorithm until a certain accuracy is achieved.
const double epsilon = 0.0001;
double guess = (double)n / 2.0;
double r = 0.0;
while( fabs(guess * guess - (double)n) > epsilon )
{
r = (double)n / guess;
guess = (guess + r) / 2.0;
}
The Babylonian Algorithm seems incorrect to me, it should be like this,
int i;
float n,guess=1;
printf("\nEnter the Number: ");
scanf("%f",&n);
for(i=0;i<PRECISION;i++)
{
guess=(guess+n/guess)/2;
}
printf("\nThe Square root of %f is %f",n,guess);
There are other possible errors also in your program,
There might be the problem of integer division,
The line double guess = n / 2;
should be double guess = (double) n / 2;
Also the printf() should be printf("%lf",guess);