Why doesn't it work correctly?

+3 votes
asked Oct 24 by Lata (640 points)
Hi there!

I was set to find minimum value of the function (y) wlile argument (x) is 0.3<=x<=3.3. However, my program says that minimum value is  2.255976 and the argument is 3.500000! You see, it can't be the right answer.

I'm giving the code below. If you see any mistake, tell me please. Thank you in advance and have a nice day!

#define A 13.8
#define B 1.26
#define X1 0.3
#define X2 3.3
#define DX 0.25
int main(void)
{
    float x, y, ymin, xmin;
    y=(A*sin(X1))/(sqrt(X1)+B);
    ymin=y;
    for(x=0.3;x<=3.3;x+=0.25) {
        y=(A*sin(x))/(sqrt(x)+B);
        if(y<ymin){
            y=ymin;
            x=xmin;
        }
    }
    printf("Найменше значення функції=%f і відповідне їй значення аргумента=%f\t", ymin, xmin);
    return 0;
}

1 Answer

+1 vote
answered Oct 24 by xDELLx (4,800 points)
selected Oct 24 by Lata
 
Best answer

float x, y, ymin, xmin;

This line leave all variables unititalised so they have random (incorrect) values

After 10 iterations of the for loop , the value of y goes on decreasing & it finally triggers the below code to reset values of x,y with thier prev min values.

 if(y<ymin){
   y=ymin;
   x=xmin;   //----> so x is initialised to random value ,a predefined min value of x is required
}

Initialisaing the  correct values should get the code going .

Also add these headers

#include <stdio.h>
#include <math.h>

commented Oct 24 by Lata (640 points)
Hello xDELLx! Thank you for your answer! Of course, I didn't forget about the headers, I just didn't copy them.
Concerning initialising the variables, I have edited this code, but it doesn't work at all. I used the other compiler codepad, but the code doesn't work at all. This is my new code:
#include <stdio.h>
#include <math.h>
#define A 13.8
#define B 1.26
#define X1 0.3
#define X2 3.3
#define DX 0.25
int main(void)
{
    float x, y, ymin, xmin;
    y=(A*sin(X1))/(sqrt(X1)+B);
    ymin=y;
    xmin=X1;
    for(x=0.3;x<=3.3;x+=0.25) {
        y=(A*sin(x))/(sqrt(x)+B);
        if(y<ymin){
            y=ymin;
            x=xmin;
        }
    }
    printf("Найменше значення функції=%f і відповідне їй значення аргумента=%f\t", ymin, xmin);
    return 0;
}

And this what the CodePad compiler says to me:
In function `main':
undefined reference to `sin'
undefined reference to `sin'.
Tell me please what to do if you know. Thanks again and have a nice day)
commented Oct 25 by xDELLx (4,800 points)
edited Oct 25 by xDELLx
"undefined reference" typically r related  to linker error.
Syntatically code is fine , but linker doesnt know where sin function is present.
On your make file , compiler command try adding math lib to the compiler option.
On gcc/g++ u can use  "-lm" option
eg: gcc my_prog.c -lm


I tried here :https://onlinegdb.com/ry7OVCzdw (Use the fork option please to debug)
code compiles & runs fine , un-fortunately infinitely.

To debug ,use "Debug/F8"
On the output window (Black screen) type "b 15" <hit "Enter"> (It will set a breakpoint on Line #15)
Then type " r "   <hit "Enter">  (This will start running ur code )
Then type "display x"   <hit "Enter">
Then type "display y"   <hit "Enter">
Then type "display ymin"   <hit "Enter"> (display commands will show values of x,y,ymin variables after each iteration)

then keep on hitting the "c" character , it will continue exec code till the next break point is hit.

From what i see in  ur code is the value of x never goes above 2.79999995 & so the for loop never terminates.

Try the above steps & figure out if the logic seems off !!
commented Oct 25 by Lata (640 points)
Thank you Dell! I tried to debug this code and I tried the above steps but still... The program never ends)
commented Oct 26 by xDELLx (4,800 points)
edited Oct 26 by xDELLx
https://www.onlinegdb.com/HJ68NZEdP --> is this what you wanted.

 if(y<ymin){              // is curr value of y less than  the prev min value of y
            ymin = y ;     //  if yes we found a new min value of y , reset the min-y value
            //xmin = x;  // Not sure if this is reqd as the loop gives it a min value of 0.3 & inc it till 3.3
}

The final result below:
Найменше значення функції=-0.707566 і відповідне їй значення аргумента=0.300000

If yes ,  the comparison of y, ymin was off, the "ymin" was being reset errorenuosly.
commented Oct 26 by Lata (640 points)
Yes, this what I wanted. Thank you very much! I appreciate your help)
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...