LANGUAGE C-The determination of the monthly interest rate by determining the positive real root of the function

Asked

Viewed 45 times

1

Hello, I need help with a language activity c. The program needs to read the amount that would originally be paid in a certain amount, the number of installments in which it was paid, and the amount of installments. From these values you need to calculate the interest rate by the function root f(j)=somatório-(valor do bem/valor da prestação). Summation is 1-(1/(1+j))^n/1-(1/(1+j)) when j>0, where j is the interest rate and n number of instalments. To find the root of the function it is necessary to use the Muller method. However, the interest of the astronomical, and I already moved everything and does not come out. If someone can help find the mistake.

The code is:

(ignore the other functions power, absolute value, relative error and square root, is that can not use the Math.h. The important f and interest)

#include <stdio.h>
#include <stdlib.h>
#define TOL 0.000000001


double potencia(double x, int n)

double valor_absoluto(double x)

double erro_relativo(double x, double y)

double raiz_quadrada(double x)


double f(double j,double vbem, int nprest, double prest)
{
    double somatorio, funcao;

    if (j==0)
    {
        somatorio=nprest;
        funcao=somatorio-(vbem/prest);
    }
    else
    {
        somatorio=(1-(potencia(1/(1+j),nprest)))/(1-(1/(1+j)));
        funcao=somatorio-(vbem/prest);
    }

    return funcao;
}

double juros (double vbem, int nprest, double prest)
{
double x0=0, x1, fx0, fx1, x2, fx2, fx3, h1, h2, q2, a, b, c, x3, esp, fx=1, resp, qlinha, qlinhalinha;

x1=0.3*(prest/(vbem-prest));
x2=prest/(vbem-prest);
fx0=f(x0,vbem,nprest,prest);
fx1=f(x1,vbem,nprest,prest);
fx2=f(x2,vbem,nprest,prest);
h1=x1-x0;
h2=x2-x1;
q2=h2/h1;
a=(q2*fx2)-(q2*(1+q2)*fx1)+((potencia(q2,2))*fx0);
b=((2*q2+1)*fx2)-((potencia((1+q2),2))*fx1)-((potencia(q2,2))*fx0);
c=(1+q2)*fx2;
qlinha=(-2*c)/(b+(raiz_quadrada(b*b-(4*a*c))));
qlinhalinha=(-2*c)/(b-(raiz_quadrada(b*b-(4*a*c))));
if (valor_absoluto(qlinha)>valor_absoluto(qlinhalinha))
{
    x3=x2+(h1*qlinhalinha);
}
else
{
    x3=x2+(h1*qlinha);
}
fx3=f(x3,vbem,nprest,prest);
esp=erro_relativo(x3, x2);
x0=x1;
x1=x2;
x2=x3;
resp=x3;

if (fx0==0)
{
    fx=fx0;
    resp=x0;
}

if (fx1==0)
{
    fx=fx1;
    resp=x1;
}

if (fx2==0)
{
    fx=fx2;
    resp=x2;
}

while (esp>=TOL && fx!=0)
{
    fx0=f(x0,vbem,nprest,prest);
    fx1=f(x1,vbem,nprest,prest);
    fx2=f(x2,vbem,nprest,prest);
    h1=x1-x0;
    h2=x2-x1;
    q2=h2/h1;
    a=(q2*fx2)-(q2*(1+q2)*fx1)+((potencia(q2,2))*fx0);
    b=((2*q2+1)*fx2)-((potencia((1+q2),2))*fx1)-((potencia(q2,2))*fx0);
    c=(1+q2)*fx2;
    qlinha=(-2*c)/(b+(raiz_quadrada(b*b-(4*a*c))));
    qlinhalinha=(-2*c)/(b-(raiz_quadrada(b*b-(4*a*c))));
    if (valor_absoluto(qlinha)>valor_absoluto(qlinhalinha))
    {
        x3=x2+(h1*qlinhalinha);
    }
    else
    {
        x3=x2+(h1*qlinha);
    }

    fx3=f(x3,vbem,nprest,prest);
    esp=erro_relativo(x3, x2);
    x0=x1;
    x1=x2;
    x2=x3;
    resp=x3;

    if (fx0==0)
    {
        fx=fx0;
        resp=x0;
    }

    if (fx1==0)
    {
        fx=fx1;
        resp=x1;
    }

    if (fx2==0)
    {
        fx=fx2;
        resp=x2;
    }
}
return resp;
}


int main()
{
    int nprest=1;
    double vbem=1,prest=1;

    while (vbem>0)
    {
        printf("Digite valor do bem,numero de prestacoes e valor das prestacoes=");
        scanf("%lf %lf %lf", &vbem, &nprest, &prest);
        printf("juros= %lf\n", juros(vbem,nprest,prest));
    }

    system("PAUSE");
    return 0;
}
  • 1

    Hello Julia! Welcome! All right? I know you can’t use the functions of the Math library. But if you use them, calculate right? Mainly the square root function sqrt? I tried running your code and did not compile in C99. Comma points are missing in function settings ... (just for you to adjust).

  • Hi, thank you so much! Everything yes and with you? I tested the separate functions and they are right, I think the error is in the functions f or interest.

  • You have not posted your function raiz_quadrada, you have made sure that it properly treats an eventual negative parameter?

  • Jeez, I had to use an estimate, but it doesn’t go with negative anyway. The root gets:double raiz_quadrada(double x){&#xA; double esp=1, x0, x1;&#xA;&#xA; x0=x;&#xA;&#xA; while(esp>=TOL)&#xA; {&#xA; x1=x0-(((x0*x0)-x)/(2*x0));&#xA; esp=valor_absoluto(((x1-x0)/x1));&#xA; x0=x1;&#xA; }&#xA; return x1;&#xA;}

  • Another point is that in its function f your parameter j, declared with double, is compared with 0 (if (j==0)), make equal comparisons to representations of floating point numbers (inherently inaccurate by definition) is not advisable as it may lead to unwanted results.

  • Then in case it is better to use != and exchange formulas?

  • Julia a cosmetic detail, but important... when working with monetary values (for exercises all right), but when reading the scanf do not use %lf for reading integers, use %d. Another important point is %.2f (for two decimal places of precision in double) just to illustrate ok. But your problem is not there. Just a cosmetic detail. But important.

  • Okay, thank you so much @Matthew

  • A nice detail for you to edit in the question is: the value of the well informed is the present value or future value? Simple: how much the property/be is worth today or how much is being paid in the act... there is a very interesting difference in clarifying this.

  • @Matthew true, in case it is the amount that should pay, because it is installments and in the end the value is higher

  • That is the GIFT value (which he is paying at the time of purchase) right?

  • Working with floating point has its peculiarities. For example. the values 0.99999999 and 1.0000000001 should be considered as 1? In terms of floating point are different from 1.

  • @Matthew like this, what do you think you’ll pay, because how can have interest in the end the value is higher

Show 8 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.