使用 Octave 绘制 3D 函数

发布于 2024-11-13 11:25:09 字数 912 浏览 0 评论 0原文

我在绘制 3d 函数时遇到问题 - 当我输入数据时,我会得到一个线性图,并且如果我手动执行计算,这些值不会相加。我认为问题与使用矩阵有关。

INITIAL_VALUE=999999;
INTEREST_RATE=0.1;
MONTHLY_INTEREST_RATE=INTEREST_RATE/12;

# ranges
down_payment=0.2*INITIAL_VALUE:0.1*INITIAL_VALUE:INITIAL_VALUE;
term=180:22.5:360;

[down_paymentn, termn] = meshgrid(down_payment, term);

# functions
principal=INITIAL_VALUE - down_payment;

figure(1);
plot(principal);
    grid;
    title("Principal (down payment)");
    xlabel("down payment $");
    ylabel("principal $ (amount borrowed)");

monthly_payment = (MONTHLY_INTEREST_RATE*(INITIAL_VALUE - down_paymentn))/(1 - (1 + MONTHLY_INTEREST_RATE)^-termn);

figure(2);
mesh(down_paymentn, termn, monthly_payment);
    title("monthly payment (principal(down payment)) / term months");
    xlabel("principal");
    ylabel("term (months)");
    zlabel("monthly payment");

正如我所说,第二个图的情节并不像我预期的那样。如何更改公式才能正确渲染?

I am having a problem graphing a 3d function - when I enter data, I get a linear graph and the values don't add up if I perform the calculations by hand. I believe the problem is related to using matrices.

INITIAL_VALUE=999999;
INTEREST_RATE=0.1;
MONTHLY_INTEREST_RATE=INTEREST_RATE/12;

# ranges
down_payment=0.2*INITIAL_VALUE:0.1*INITIAL_VALUE:INITIAL_VALUE;
term=180:22.5:360;

[down_paymentn, termn] = meshgrid(down_payment, term);

# functions
principal=INITIAL_VALUE - down_payment;

figure(1);
plot(principal);
    grid;
    title("Principal (down payment)");
    xlabel("down payment $");
    ylabel("principal $ (amount borrowed)");

monthly_payment = (MONTHLY_INTEREST_RATE*(INITIAL_VALUE - down_paymentn))/(1 - (1 + MONTHLY_INTEREST_RATE)^-termn);

figure(2);
mesh(down_paymentn, termn, monthly_payment);
    title("monthly payment (principal(down payment)) / term months");
    xlabel("principal");
    ylabel("term (months)");
    zlabel("monthly payment");

The 2nd figure like I said doesn't plot like I expect. How can I change my formula for it to render properly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

霊感 2024-11-20 11:25:09

我尝试了您的脚本,并收到以下错误:

error: octave_base_value::array_value(): wrong type argument `complex matrix'
...

您的 monthly_ payment 是一个复杂的矩阵(它不应该是)。

我猜问题出在幂运算符 ^ 上。您应该使用 .^ 进行逐个元素的操作。

从文档中:

x^y
x ** y
电力操作员。如果 x 和 y 都是标量,则此运算符返回 x 的 y 次方。如果 x 是标量且 y 是方阵,则使用特征值展开计算结果。如果x是方阵。如果 y 是整数,则通过重复乘法计算结果;如果 y 不是整数,则通过特征值展开计算结果。如果 x 和 y 都是矩阵,则会产生错误。

该运算符的实现需要改进。

x .^ y
x .** y
逐个元素的幂运算符。如果两个操作数都是矩阵,则行数和列数必须一致。

I tried your script, and got the following error:

error: octave_base_value::array_value(): wrong type argument `complex matrix'
...

Your monthly_payment is a complex matrix (and it shouldn't be).

I guess the problem is the power operator ^. You should be using .^ for element-by-element operations.

From the documentation:

x ^ y
x ** y
Power operator. If x and y are both scalars, this operator returns x raised to the power y. If x is a scalar and y is a square matrix, the result is computed using an eigenvalue expansion. If x is a square matrix. the result is computed by repeated multiplication if y is an integer, and by an eigenvalue expansion if y is not an integer. An error results if both x and y are matrices.

The implementation of this operator needs to be improved.

x .^ y
x .** y
Element by element power operator. If both operands are matrices, the number of rows and columns must both agree.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文