Question

Hello i have code for parameter estimation in C language. The problem is that the code...

Hello i have code for parameter estimation in C language. The problem is that the code is developed for floating point numbers and i want it for integer values The code is

#include "stdio.h"
#include "conio.h"
#include "math.h"
#define N 30
double g(double b, double s[], double sn, double sk);
double gdash(double b, double s[], double sn);
main()
{
int i;
double sk=0;
double s_avg,g1,g2,a;
double

s[N]={30.02,31.46,53.93,55.29,58.72,71.92,77.07,80.9,101.9,114.87,115.34,
121.57,124.96,134.07,136.25,151.78,177.5,180.29,182.21,186.34,256.81,
273.88,277.87,453.93,535.0,537.27,552.9,673.68,704.49,738.68};
double b[N];
clrscr();
sk=0, s_avg=0.0;
printf("\n Input observations :\n");
for(i=0;i<N;i++)
sk=sk+s[i];
s_avg=sk/N;
b[0]=log(4)/s_avg;
printf("\nb=%f\n",b[0]);
i=-1;
do
{
i=i+1;
g1=g(b[i],s,s[N-1],sk);
g2=gdash(b[i],s,s[N-1]);
b[i+1]=b[i]-(g1/g2);
printf("\nb[%d]= %lf b[%d]= %lf",i,b[i],i+1,b[i+1]);
printf("\n\t\t\t|b[%d]-b[%d]| = %lf",i+1,i,fabs(b[i+1]-b[i]));
}while((fabs(b[i+1]-b[i])>=0.0001));
a=((N)/(1-exp(-b[i+1]*(s[N-1]))));
printf("\n\nb[%d] = %lf is the M.L.E of b = %lf a=%lf",i+1,b[i+1],b[i+1],a);
getch();
}
double g(double b, double s[N], double sn, double sk)
{
int k;
double v3=0.0,c3,c4,g_val;
for(k=0;k<N-1;k++)
v3=v3+(s[k]);
c3=(N*sn*(exp(-b*sn)))/(1-exp(-b*sn));

g_val=v3-(N/b)+c3;
return g_val;
}
double gdash(double b,double s[N], double sn)
{
double gdash_val,c3;
c3=(N*sn*sn)*((1/(1-exp(-b*sn)))+(exp(-b*sn))/
(1-exp(-b*sn))*(1-exp(-b*sn)))*exp(-b*sn);
gdash_val=(N/(b*b))+c3;
return gdash_val;
}
In this code the double array has declared for double

s[N]={30.02,31.46,53.93,55.29,58.72,71.92,77.07,80.9,101.9,114.87,115.34,
121.57,124.96,134.07,136.25,151.78,177.5,180.29,182.21,186.34,256.81,
273.88,277.87,453.93,535.0,537.27,552.9,673.68,704.49,738.68};
where as i want values to be entered in
s[n] are {1,3,8,9,11,16,19,25,27,29,32,32,26,38,39,39,41,42,42}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// I HAVE ONLY COMMENTED THE CHANGES I HAVE DONE IN THE CODE

#include "stdio.h"

#include "conio.h"

#include "math.h"

#define N 30

//here we have changed the signature of function from

//(double b, double s[], double sn, double sk) to (double b, int s[], int sn, double sk)

//in order to accept integer values of s[n].

double g(double b, int s[], int sn, double sk);

//here we have changed the signature of function from

//(double b, double s[], double sn) to (double b, int s[], int sn)

//in order to accept integer values of s[n].

double gdash(double b, int s[], int sn);

//I have changed main() to int main due to compiler issues

int main()
{

int i;

double sk,s_avg,g1,g2,a;

//This is the int s[n] input as required by you

int s[N]={30,31,53,55,58,71,77,80,101,114,115,121,124,134,136,151,177,180,182,186,256,273,277,453,535,537,552,673,704,738};

//Your unchanged code starts here.

double b[N];

sk=0, s_avg=0.0;

printf("\n Input observations :\n");

for(i=0;i<N;i++) sk=sk+(s[i]*1.0); //I have multiplied s[i] to 1.0 to explicitly type cast s[i] as a double.

s_avg=sk/N;

b[0]=log(4)/s_avg;

printf("\nb=%f\n",b[0]);

i=-1;

do

{

i=i+1;

g1=g(b[i],s,s[N-1],sk);

g2=gdash(b[i],s,s[N-1]);

b[i+1]=b[i]-(g1/g2);

printf("\nb[%d]= %lf b[%d]= %lf",i,b[i],i+1,b[i+1]);

printf("\n\t\t\t|b[%d]-b[%d]| = %lf",i+1,i,fabs(b[i+1]-b[i]));

}while((fabs(b[i+1]-b[i])>=0.0001));

a=((N)/(1-exp(-b[i+1]*(s[N-1]))));

printf("\n\nb[%d] = %lf is the M.L.E of b = %lf a=%lf",i+1,b[i+1],b[i+1],a);

return 0;

//Your unchanged code ends here

}

double g(double b, int s[N], int sn, double sk)

{

int k;

double v3=0.0,c3,c4,g_val;

for(k=0;k<N-1;k++) v3=v3+(s[k]*1.0); //I have multiplied s[k] to 1.0 to explicitly type cast s[k] as a double.

double x=sn*1.0; //I have multiplied sn to 1.0 to explicitly type cast sn as a double.

c3=(N*x*(exp(-b*x)))/(1-exp(-b*x));

g_val=v3-(N/b)+c3;

return g_val;

}

double gdash(double b,int s[N], int sn)

{

double gdash_val,c3;

double x=sn*1.0; //I have multiplied sn to 1.0 to explicitly type cast sn as a double.

c3=(N*x*x)*((1/(1-exp(-b*x)))+(exp(-b*x))/(1-exp(-b*x))*(1-exp(-b*x)))*exp(-b*x);

gdash_val=(N/(b*b))+c3;

return gdash_val;

}

Add a comment
Know the answer?
Add Answer to:
Hello i have code for parameter estimation in C language. The problem is that the code...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hello i have code for parameter estimation in C language. The problem is that the code is developed for floating point numbers and i want it for integer values The code is #include "stdio.h" #...

    Hello i have code for parameter estimation in C language. The problem is that the code is developed for floating point numbers and i want it for integer values The code is #include "stdio.h" #include "conio.h" #include "math.h" #define N 30 double g(double b, double s[], double sn, double sk); double gdash(double b, double s[], double sn); main() { int i; double sk=0; double s_avg,g1,g2,a; double s[N]={30.02,31.46,53.93,55.29,58.72,71.92,77.07,80.9,101.9,114.87,115.34, 121.57,124.96,134.07,136.25,151.78,177.5,180.29,182.21,186.34,256.81, 273.88,277.87,453.93,535.0,537.27,552.9,673.68,704.49,738.68}; double b[N]; clrscr(); sk=0, s_avg=0.0; printf("\n Input observations :\n"); for(i=0;i<N;i++) sk=sk+s[i];...

  • C code program help. I am trying to find the total parallel resistive load. This is...

    C code program help. I am trying to find the total parallel resistive load. This is what I have so far. It does run, it does ask for numbers. But it doesn't calculate. It just comes out "The parallel is: 0.00". I have tried 3, 4, 5 and 3.2, 4.3, 5.4. Still only shows 0.00. I have tried several combinations of putting different data types in both main.c and the rest of it. Any help or ideas would be helpful....

  • I am told to find the median of random inputs using if statements in C #include...

    I am told to find the median of random inputs using if statements in C #include <stdio.h> #include <stdlib.h> int main() { double a, b, c, d; scanf("%lf", &a); scanf("%lf", &b); scanf("%lf", &c); if (a<b && b<c) d = b; printf("%.0f\n", d); else if (b<a && a<c) d = a; printf("%.0f\n", d); else d = c; printf("%.0f\n", d); return 0; } That is my code, I keep getting an error for the else if and else saying there is no...

  • i'm having trouble making an else statement that will allow the program to add and output...

    i'm having trouble making an else statement that will allow the program to add and output the average the valid numbers inputed into the program. Here's my output: Enter Score 1:36 Enter Score 2:-1 Invalid Input Enter Score 2:89.5 Enter Score 3:42 Enter Score 4:66.3 Enter Score 5:93 You entered: 36.000000, 89.500000, 42.000000, 66.300000, 93.000000 Total Score: 362.800000 Average Score: 72.560000 Max Score: 93.000000 Min Score: 36.000000 Here's my code: #include <stdio.h> int main(void) { double score[5]; double min; double...

  • Hello, I have my piece of C programming code and I have a pointer initialized to...

    Hello, I have my piece of C programming code and I have a pointer initialized to point to my array and I need help to display the contents of my array using a while loop or do-while loop. The stop condition should be when the pointer reaches '\0'. The code is below: #include <stdio.h> int main () {    char array[80]; printf("Enter a string: "); scanf("%[^\n]", &array); printf("%s\n", array); char * p;    p = array;         }

  • Hello, I am having trouble with a problem in my C language class. I am trying...

    Hello, I am having trouble with a problem in my C language class. I am trying to make a program with the following requirements: 1. Use #define to define MAX_SIZE1 as 20, and MAX_SIZE2 as 10 2. Use typedef to define the following struct type: struct {     char name[MAX_SIZE1];     int scores[MAX_SIZE2]; } 3. The program accepts from the command line an integer n (<= 30) as the number of students in the class. You may assume that the...

  • I need someone to fix my C Program Code Firstly I will post the Q and...

    I need someone to fix my C Program Code Firstly I will post the Q and the input and the outputs Then the code Secondly the code =================================== #include <stdio.h> #include <math.h> int main() {    double a,b,c,d,l,x,x_0,x_1,x_mid,p_x,p_0,p_1,p_mid,E=1e-6; int i1,i2;          printf("Enter a,b,c,d of ax^3+bx^2+cx+d=0: "); scanf("%lf%lf%lf%lf",&a,&b,&c,&d); x_0=-50;x_1=-50;x=-50;p_0=a*(pow(x_0,3))+b*(pow(x_0,2))+c*x_0+d;i1=0;i2=0; do{        p_x=a*x*x*x+b*x*x+c*x+d;        l=p_x*p_0;        if(l<0){        x_1=x;}        i1=i1+1;x++;}        while(fabs(p_x)>=E && x<50);           if(fabs(p_x)<E){    printf("the polynominal has root...

  • Below is the code for two programs in C language, I was trying to make proper...

    Below is the code for two programs in C language, I was trying to make proper comments within the code so that someone can understand it. Please review the comments within the code and let me know if it makes good sense. Let me know if I need to make any changes to the comments! Thank you. PROGRAM 1: /* program calculates harmonic mean for 10 numbers entered by the user */ /* if illegal value (x<0) is entered, the...

  • PROGRAMING IN C. PLEASE HELP FIX MY CODE TO FIT THE FOLLOWING CRITERIA: 1.)Read your stocks...

    PROGRAMING IN C. PLEASE HELP FIX MY CODE TO FIT THE FOLLOWING CRITERIA: 1.)Read your stocks from your mystocks.txt file. You can use this information for the simulator. 2.)The stock price simulator will return the current price of the stock. The current prices is the prices of the requested ticker + a random number. 3.)We will assume that the stock price remains constant after the price check till your trade gets executed. Therefore, a buy or a sell order placed...

  • URGENT. Need help editing some C code. I have done most of the code it just...

    URGENT. Need help editing some C code. I have done most of the code it just needs the following added: takes an input file name from the command line; opens that file if possible; declares a C struct with three variables; these will have data types that correspond to the data types read from the file (i.e. string, int, float); declares an array of C structs (i.e. the same struct type declared in point c); reads a record from the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT