gpt4 book ai didi

perl - perl 中的牛顿法

转载 作者:行者123 更新时间:2023-12-02 07:27:15 25 4
gpt4 key购买 nike

好吧,在我的数学课上,我们被要求编写一个程序来执行和打印牛顿法,直到值收敛并且我们有函数的根。起初我以为这很容易。直到我无法获得第二次使用的第一次使用的值。我的语言知识是基本的。非常基本,所以您将要看到的可能并不漂亮。

#!usr/bin/perl

use PDL;

print "First guess? (this is x0)\n";
$xorig = <>;

do {
&fx;
} until ($fex == 0);

sub fx {

if ($xn == 0) {
$x = $xorig;
}
else {
$x = $xn;
}

print "What is the coefficient (for each factor) of your function?\n";
$fcx = <STDIN>;
push @coefficient_of_x, $fcx;

print "... times x to the (enter exponent, if no exponent, enter 1. if no x, enter 0)?\n";
$fex = <STDIN>;
push @exponent_x, $fex;

chomp ($fcx, $fex, $x, $xorig);

$factor = $fcx * ($x ** $fex);
push @fx, $factor;
}

my $fx = 0;
foreach my $variable (@fx) {
$fx = $variable + $fx #THIS PROVIDES A VALUE FOR THE GIVEN F(X) WITH A GIVEN X VALUE
}
print "f($x)=$fx\n";

do {
&fprimex;
} until ($fprimeex == 0);

sub fprimex {

if ($xn == 0) {
$x = $xorig;
}
else {
$x = $xn;
}

print "What is the coefficient (for each factor) of your derivative function?\n";
$fprimecx = <STDIN>;
push @coefficient_of_fpx, $fprimecx;

print "... times x to the (enter exponent, if no exponent, enter 1. if no x, enter 0)?\n";
$fprimeex = <STDIN>;
push @exponent_fpx, $fprimeex;

chomp ($fprimecx, $fprimeex, $x, $xorig);

$factorprime = $fprimecx * ($x ** $fprimeex);
push @fprimex, $factorprime;
}

$fprimex = 0;
foreach my $variableprime (@fprimex) {
$fprimex = $variableprime + $fprimex #THIS PROVIDES A VALUE FOR THE GIVEN F'(X) WITH THAT SAME X VALUE
}
print "f'($x)=$fprimex\n";

sub x0 {
$xn = $xorig - $fx / $fprimex; #THIS IS NEWTON'S METHOD EQUATION FOR THE FIRST TIME
push @newxn, $xn;
print "xn ia $xn\n";
}

&x0;

foreach $value (@exponent_x) {
$exponent_x = $xn ** $value;
push @part1, $exponent_x;
$part1 = @part1;
}

foreach $value2 (@coefficient_of_x) {
$part2 = $value2 * @part1;
push @final1, $part2;
}

print "@part1\n";
print "@final1\n";

基本上是我先问第一个猜测。我使用这个值来定义 f(x) 的系数和指数,以根据给定的 x 获得 f(x) 的值。我为 f'(x) 再做一次。然后我第一次执行牛顿法得到新值xn。但是我很难获得 f(xn) 和 f'(xn) 的值,这意味着我无法获得 x(n+1) 并且无法继续牛顿的方法。我需要帮助。

最佳答案

欢迎使用 Perl。

我强烈建议对您的代码进行以下更改:

  1. 始终包含 use strict;use warnings;在每个 Perl 脚本中。
  2. 始终 chomp您从 STDIN 输入的内容作为您的输入:

    chomp( my $input = <STDIN> );
  3. 不要不必要地创建子例程,尤其是对于像这样的一次性脚本。

  4. 而不是使用 statement modifier do 的形式,我建议使用无限 while用循环控制语句退出:

    while (1) {

    last if COND;
    }
  5. 最后,由于多项式的系数都与 X 的指数相关联,我建议使用 %hash 来方便地保存这些值。

如图所示:

#!usr/bin/env perl
use strict;
use warnings;

print "Build your Polynomial:\n";

my %coefficients;

# Request each Coefficient and Exponent of the Polynomial
while (1) {
print "What is the coefficient (for each factor) of your function? (use a bare return when done)\n";
chomp( my $coef = <STDIN> );

last if $coef eq '';

print "... times x to the (enter exponent, if no exponent, enter 1. if no x, enter 0)?\n";
chomp( my $exp = <STDIN> );

$coefficients{$exp} = $coef;
}

print "\nFirst guess? (this is x0)\n";
chomp( my $x = <> );

# Newton's Method Iteration
while (1) {
my $fx = 0;
my $fpx = 0;

while ( my ( $exp, $coef ) = each %coefficients ) {
$fx += $coef * $x**$exp;
$fpx += $coef * $exp * $x**( $exp - 1 ) if $exp != 0;
}

print " f(x) = $fx\n";
print " f'(x) = $fpx\n";

die "Slope of 0 found at $x\n" if $fpx == 0;

my $new_x = $x - $fx / $fpx;

print "Newton's Method gives new value for x at $new_x\n";

if ( abs($x - $new_x) < .0001 ) {
print "Accuracy reached\n";
last;
}

$x = $new_x;
}

关于perl - perl 中的牛顿法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26832407/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com