How to Calculate Xi in C Program for Mathematic Function?

  • Thread starter Thread starter sulev8
  • Start date Start date
  • Tags Tags
    Program
Click For Summary

Discussion Overview

The discussion revolves around implementing a specific mathematical function in C programming, particularly focusing on calculating a series of values (x1 to x15) based on user-defined parameters. The problem involves understanding how to structure the calculation logic in code, including the initialization of variables and handling of mathematical operations.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes a mathematical function where x values increase based on a formula and seeks assistance in translating this into C code.
  • Another participant requests the original poster to share their code attempt for more targeted help, emphasizing that direct answers to homework questions are not provided.
  • A participant shares a code snippet but expresses uncertainty about how to ensure that x1 remains constant and that calculations start from x2.
  • Concerns are raised about the initialization of the loop index and the potential for division by a very small number, which could lead to errors in calculations.
  • Suggestions are made to use a for loop instead of a while loop for better control over the index variable.

Areas of Agreement / Disagreement

Participants generally agree on the need for clarity in variable initialization and the structure of the loop, but there is no consensus on the best approach to implement the calculations without errors.

Contextual Notes

There are unresolved issues regarding the initialization of variables c and h, as well as the handling of edge cases when calculating square roots of small numbers. The discussion does not resolve how to maintain the value of x1 while calculating subsequent x values.

sulev8
Messages
5
Reaction score
0
I have a problem with c program calculation part . i have a mathematic funxion which equals to y. And i need to calculate the value. While x has a conditon that's value will increase 15 times. Like x1=a ; x2=a+h x3=a+h+(c*h) ; x4=a+h+(c*h)+((c*c)*h) ; x5=a+h+(c*h)+((c*c)*h)+((c*c*c)*h) and so on to X15. x1 which = A ,c,h and Ymin value is inserted by the program user. I Have come to conclusion , that i can write a formula for x value increase , which is Xi=Xi-1 +(c^i-2 * H) and it goes from x2 to x15 .
My problem is that how i can write this to c language , what line i should write that the program would calculate x1 by just adding to the y= formula and from x2 to x15 it would use the condition that Xi=Xi-1 +(c^i-2 * H).
And please don't give me just advice with this , i would just like to know how should i mark this in C language.
 
Physics news on Phys.org
sulev8 said:
I have a problem with c program calculation part . i have a mathematic funxion which equals to y. And i need to calculate the value. While x has a conditon that's value will increase 15 times. Like x1=a ; x2=a+h x3=a+h+(c*h) ; x4=a+h+(c*h)+((c*c)*h) ; x5=a+h+(c*h)+((c*c)*h)+((c*c*c)*h) and so on to X15. x1 which = A ,c,h and Ymin value is inserted by the program user. I Have come to conclusion , that i can write a formula for x value increase , which is Xi=Xi-1 +(c^i-2 * H) and it goes from x2 to x15 .
My problem is that how i can write this to c language , what line i should write that the program would calculate x1 by just adding to the y= formula and from x2 to x15 it would use the condition that Xi=Xi-1 +(c^i-2 * H).
And please don't give me just advice with this , i would just like to know how should i mark this in C language.

Moved to Homework Help forums.

sulev8, we do not give out answers to homework questions. Show us your attempt at the program first, and then we can offer tutorial help.
 
i don't want u to solve the whole program , this is just the part of a program , i am asking how i should write these lines into my program .Its the calculation part of the program , and i need some help with it. As i wrote i gave out the solution , now i need help how to put it down , just these few lines.
As i see it was another useless attempt to seek help from here , its like a basic answer in this forum.// calculation part
void calculation (float xva[15], float yva[15])
{
int i;
i=0;
while (i<15)
{
xva=xva[i-1]+(pow(c,i-2)*h);
if (xva!=0)
{
/* if x aint 0 , calculate value*/
yva=(xva*xva)+(xva/2)-sqrt(1/(2*xva));
}
i++;
}
}this is what i have made , but i don't get if the program takes into count that x1 stays same as A and that it ( xva=xva[i-1]+(pow(c,i-2)*h);) should start from x2 to x15
 
why can't u just comment my work ,and help me by giving me how it should be , and tell what's missing
 
xva=xva[i-1]+(pow(c,i-2)*h);

What are c and h - where are the defined, initialised ?
You can't call this with i<2, you start at i=0;
 
Last edited:
Well, one thing can cause you trouble:

sqrt(1/(2*xva));

You check for zero, but since xva is a float, it still can be a very small number (not zero) and blow up on you. Keep in mind that while a human would know that .000000000001 is basically zero, to a computer it is not.

you also don't want to initialize i to zero. Otherwise, the i-2 factor will end up at -2. You want to initialize it to 2.

Finally, use a for statement instead of a while: Like for(i=2;i < 15;i++)
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 6 ·
Replies
6
Views
2K
Replies
18
Views
3K
  • · Replies 13 ·
Replies
13
Views
2K
Replies
2
Views
2K