English 中文(简体)
采用纽顿语方法在C中解决非线性的问题?
原标题:Solving non-linear equation in C using Newton s method?

我有一个非线性方程式,定义为:p^(n-1)* [1-(1-p)^n] * r - c = 0n ,r,,>c>。 我想在C方案拟订语言中解决p的问题,并决定使用纽顿语方法。 我想知道,这是否是最合适的办法,是否有其他办法解决这一问题。 (一个重要的注是,此刻有概率,因此从0到1!)

这里是我目前的执行情况,看来对一些测试案例的工作是失败的:

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

#define ld long double
#define TOLERANCE 1e-7
#define ITERATIONS 1000

ld function(ld p, int n, int c, int r) {
    return pow(p, n - 1) * (1 - pow(1 - p, n)) * r - c;
}

ld derivative(ld p, int n, int r) {
    return r * (n - 1) * pow(p, n - 2) * (1 - pow(1 - p, n)) + r * n * pow(p, n - 1) * pow(1 - p, n - 1);
}

ld pFinder(int n, ld c, ld r) {
    ld p = 0.5;

    if (n == 0) {
        return 0;
    }

    for (int i = 0; i < ITERATIONS; i++) {
        ld fValue = function(p, n, c, r);
        ld fPrime = derivative(p, n, r);

        p = p - fValue / fPrime;

        if (fabs(fValue) < TOLERANCE) {
            return p;
        }
    }
    
    printf("Didn t converge after %d iterations", ITERATIONS);
    return -1;
}

int main() {
    int n, c, r;

    scanf("%d %d %d", &n, &c, &r);

    ld p = pFinder(n, c, r);

    printf("%.15Lf
", p);
    return 0;
}

产生这一问题的是如下投入:18 30 2,其结果为1.99936414839,该编码不在<代码>[0, 1]的有效范围之内。 我不理解为什么发生这种情况。

这些制约因素包括:1 <=n <= 20,1 <=10000,1 <= 10000,p的绝对错误可上10^-6

我赞赏对可能错失的东西以及如何改进我的法典的任何见解。

P.S.是我第一次在这里做一些事情,我很抱着新意 co,因此,我希望你不要忘记,我不会在Markdown syntax上ficient。

问题回答

<p to be in [0,1],c/r也必须在[0,1]内。 在这种情况下,如果你开始p = 1(而不是p=0.5),纽顿的方法将产生良好效果,并将很快达到预期的精确度。 远高于两节。

您的“10 302”,c/r为15,0,1]。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签