(*Define initial mortgage parameters*)
principal=100000;(*Example principal amount-adjust as needed*)initialInterestRate=0.0415;(*4.15% as a decimal*)initialTermYears=25;
initialTermMonths=initialTermYears*12;

(*Function to calculate monthly payment*)
monthlyPayment[P_,r_,n_]:=P (r/12)/(1-(1+r/12)^(-n))

(*Initial monthly payment*)
initialMonthlyPayment=monthlyPayment[principal,initialInterestRate,initialTermMonths];

(*Calculate remaining balance after one year (12 months)*)
remainingBalance[P_,r_,n_,t_]:=P (1-(1+r/12)^(-n+t))/(1-(1+r/12)^(-n))

balanceAfterOneYear=remainingBalance[principal,initialInterestRate,initialTermMonths,12];

(*Calculate new interest rate after prime rate falls by 50 basis points*)
newInterestRate=initialInterestRate-0.0050;(*50 basis points=0.50%=0.0050*)(*Calculate remaining term without extra principal payment*)remainingTermMonths[P_,r_,M_]:=-Log[1-(P (r/12))/M]/Log[1+r/12]

remainingMonthsNoExtraPayment=remainingTermMonths[balanceAfterOneYear,newInterestRate,initialMonthlyPayment];
remainingYearsNoExtraPayment=remainingMonthsNoExtraPayment/12;

Print["Remaining amortization period after rate drop (no extra payment): ",remainingYearsNoExtraPayment," years"];

(*Calculate remaining balance after 10% principal payment AND rate reduction*)
principalPayment=0.10*principal;
newBalance=balanceAfterOneYear-principalPayment;

(*Calculate new monthly payment (using initial payment as a starting point for iteration)*)
newMonthlyPayment=monthlyPayment[newBalance,newInterestRate,remainingMonthsNoExtraPayment];

(*Calculate remaining term with extra principal payment*)
remainingMonthsWithExtraPayment=remainingTermMonths[newBalance,newInterestRate,newMonthlyPayment];
remainingYearsWithExtraPayment=remainingMonthsWithExtraPayment/12;

Print["Remaining amortization period after rate drop AND 10% principal payment: ",remainingYearsWithExtraPayment," years"];

(*Verification and Output*)

Print["Initial Principal: ",principal];
Print["Initial Interest Rate: ",initialInterestRate*100,"%"];
Print["Initial Term (Years): ",initialTermYears];
Print["Initial Monthly Payment: ",initialMonthlyPayment];
Print["Balance After One Year: ",balanceAfterOneYear];
Print["New Interest Rate: ",newInterestRate*100,"%"];
Print["Principal Payment: ",principalPayment];
Print["New Balance After Principal Payment: ",newBalance];
Print["New Monthly Payment: ",newMonthlyPayment];