Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7680425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:06:42+00:00 2026-05-31T18:06:42+00:00

I am trying in C language to use the method of bisection to find

  • 0

I am trying in C language to use the method of bisection to find the roots of some equation, however when I try to write every step of this process in a file I get the problem “Segmentation fault”. This might be an idiot fault that I did, however I have been trying to solve this for a long time. I am compiling using gcc and that is the code:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define R 1.0
#define h 1.0


double function(double a);
void attractor(double *a1, double *a2, double *epsilon);


void attractor(double *a1, double *a2, double *epsilon)
{
 FILE* bisection; 
 double a2_copia, a3, fa1, fa2;

 bisection = fopen("bisection-part1.txt", "w");
 fa1 = function(*a1);
 fa2 = function(*a2);

 if(function(*a1) - function(*a2) > 0.0)
  *epsilon = function(*a1) - function(*a2);
 else
  *epsilon = function(*a2) - function(*a1);


 fprintf(bisection, "a1     a2      fa1     fa2     epsilon\n");

 a2_copia = 0.0;

 if(function(*a1) * function(*a2) < 0.0 && *epsilon >= 0.00001)
 {
  a3 = *a2 - (*a2 - *a1);
  a2_copia = *a2;  
  *a2 = a3;

  if(function(*a1) - function(*a2) > 0.0)
   *epsilon = function(*a1) - function(*a2);
  else
   *epsilon = function(*a2) - function(*a1);

  if(function(*a1) * function(*a2) < 0.0 && *epsilon >= 0.00001)
  {
   fprintf(bisection, "%.4f %.4f %.4f %.4f %.4f\n", *a1, *a2, function(*a1), function(*a1), *epsilon); 
   attractor(a1, a2, epsilon);
  } 
  else
  {
   *a2 = a2_copia;
   *a1 = a3;
   if(function(*a1) - function(*a2) > 0)
    *epsilon = function(*a1) - function(*a2);
   else
    *epsilon = function(*a2) - function(*a1);

   if(function(*a1) * function(*a2) < 0.0 && *epsilon >= 0.00001)
   {
    fprintf(bisection, "%.4f %.4f %.4f %.4f %.4f\n", *a1, *a2, function(*a1), function(*a1), *epsilon);
    attractor(a1, a2, epsilon);
   } 
  }
 }

 fa1 = function(*a1);
 fa2 = function(*a2);

 if(function(*a1) - function(*a2) > 0.0)
  *epsilon = function(*a1) - function(*a2);
 else
  *epsilon = function(*a2) - function(*a1);

 fprintf(bisection, "%.4f %.4f %.4f %.4f %.4f\n", a1, a2, fa1, fa2, epsilon); 


}

double function(double a)
{
 double fa;
 fa = (a * cosh(h / (2 * a))) - R;
 return fa;
}

int main()
{

 double a1, a2, fa1, fa2, epsilon;


 a1 = 0.1;
 a2 = 0.5;



 fa1 = function(a1);
 fa2 = function(a2);
 if(fa1 - fa2 > 0.0)
  epsilon = fa1 - fa2;
 else
  epsilon = fa2 - fa1;

 if(epsilon >= 0.00001)
 {
  fa1 = function(a1);
  fa2 = function(a2);
  attractor(&a1, &a2, &epsilon);
  fa1 = function(a1);
  fa2 = function(a2);
  if(fa1 - fa2 > 0.0)
   epsilon = fa1 - fa2;
  else
   epsilon = fa2 - fa1;

 }

 if(epsilon < 0.0001)
  printf("Vanish at %f", a2);
 else
  printf("ERROR");



 return 0;

}

Thanks anyway and sorry if this question is not suitable.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T18:06:43+00:00Added an answer on May 31, 2026 at 6:06 pm

    You have too many open files. You are calling attractor recursively, and each call will open the file bisection-part1.txt. fopen returns NULL on failure. The program terminates with a segmentation fault because you are trying to use a NULL file descriptor.

    You need to open the file once and pass its file descriptor to the attractor function:

    void attractor(FILE *bisection, double *a1, double *a2, double *epsilon) { ... }
    

    You should also use fclose to close all file(s) after you are done with them.

    The limit on the number of open files is usually 1024. It can be printed by executing ulimit -n.


    If all fprintf calls are replaced with printf, the program runs out of stack space and because of this it terminates with a segmentation fault. The program runs out of stack space because the recursion level of function attractor is too high.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying to use buildExpressionParser to parse a language, and I almost have
I'm trying to understand why one would use Spring Batch over a scripting language
I am getting start to socket use in C programming language. I am trying
I'm trying to use the method for naming a lua package after the filename
I'm trying develop a sample application with multi language files. I use: Windows 7
what i'm trying to do is to write a method in c# codebehind to
I am trying to write some complicated acceptance tests for my C# code. I
Trying to convert the following php method to use in a .less stylesheet: <?php
I'm trying to use ANTLR to create a parser for simple language using C#
i'm trying to figure out how much i can use of the javascript language

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.