Search code examples
algorithmmatlabequation-solving

Solving an equation of one unknown, given as a function


I have a homework assignment and I can't seem to figure out how to do it. I'm given everything except r. I need to use this formula, as it's saved as a function from a previous exercise. The function is:

function t = annuitet( g , T , G , r , n )
%Et annuitetslån
%g  termingebyr på kr for hvert terminforfall
%T  annuitetslån på kr
%G  etablerings gebyr
%r  årlig rente
%n  antall terminer (=antall år ganger 12)

t = g + ((T+G)*((r/12)/100))/(1-(1+(r/12)/100)^(-n*12));

end

Don't mind that it's in Norwegian.

I am given that:

T = 2000000
n = 20
G = 3000
g = 50
t = 9500

I know that r has to be 1.26%, but I can't for the life of me figure out how to manipulate this in order to make a script that will make it work.

Currently have:

T = input('Tast inn lånebeløp (kr):');
n = input('Løpetid (år):');
G = input('Etableringsgebyr (kr):');
g = input('Termingebyr (kr):');

disp('Absolutt minste terminbeløp (Rente = 0 %): 8396 kr');
t = input('Tast inn terminbeløpet du kan klare (kr):');

Thanks in advance for any hints/tips :)


Solution

  • You are trying to solve an equation with 1 unknown. The way to go about this numerically, is define an equation for the residual (that is "how bad is my error given some guess for r") and then try to minimize said residual.

    One way to do this is using fzero. Another way is by guessing r using some fine grid (e.g. 0.001:0.001:100) and looking which r gives you the lowest residual. Yet another way is by doing the former, and plotting it (a "graphical solution" of sorts).

    Since this is a homework assignment, I'm putting the rest of the solution as "hidden" text. I encourage you to read the documentation and try to figure it out on your own, then when you succeed, compare it with my answer :)

    Good luck!

    r_sol = fzero(@(r)t-annuitet( g , T , G , r , n ), 1000);
    -> r_sol = 1.264530947561246