开发者

Plotting a 3D function with Octave

开发者 https://www.devze.com 2023-03-09 23:21 出处:网络
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 matr

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("mo开发者_运维技巧nthly 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消