Your formula amounts to:
$$M = P \frac{2-(1-r)^n}{n}. \tag{1}$$
For the sake of clarity, $r$ is the effective monthly rate of interest, and $n$ is the number of months in the loan. Note that we immediately run into trouble if $r > 1$, which is certainly possible for especially usurious loans (e.g., a very short term payday loan could easily have effective annual rates of interest exceeding $300\%$). Practicality aside, the mathematics must remain valid even when the rate of interest is especially high. But in your case, if we set $r = 3$, then $(1-r)^n = (-2)^n$, which would be positive if $n$ is even and negative if $n$ is odd. This would suggest your periodic payment $M$ would be negative if the interest rate is high enough and there were an even number of payment periods!
Here is a more realistic example, in which we might let $r = 0.05$ and $n = 100$. Then on a principal of $P = 100000$, the correct formula gives $M = 5038.31$, whereas yours is much smaller, only $M = 1994.08$. As you can see, the deviation is substantial.
So now that we know that this formula cannot be mathematically correct, why does this model you propose fail? The problem is that you are confusing different time values of money, and not taking into account the compounding of interest.
In particular, when you say that after $n$ months, the amount of principal remaining is $(1-r)^n P$, this makes no sense, because it means that the loan is never extinguished; the quantity $(1-r)^n$ for any $0 < r < 1$ and $n > 0$ is always positive. For example, if $r = 0.0001$ and $n = 1000$, $(1-r)^n \approx 0.904833$, meaning that your formula would say that over $90\%$ of the principal is still remaining after $1000$ payments.
Instead, what you should write is an equation of value that matches the present value of the periodic payments made in the future, against the present value of the principal of the loan. What I mean by this is that if $r$ is the monthly rate, then a payment of $M$ made $t$ months into the future would have the present value $$M(1+r)^{-t}.$$ Then the sum of these "discounted" payments must equal the total principal of the loan.
Alternatively, we can say that if $M$ is the monthly payment, then after the first month has passed, the loan has accrued interest of $rP$, hence the total outstanding balance is $P + rP = (1+r)P$, and you pay off $M$ of this, so the remaining balance is $(1+r)P - M$. Then another month passes, at which point the outstanding balance has increased by a factor of $1+r$, and you pay off another month:
$$(1+r)((1+r)P - M) - M = (1+r)^2P - (1+r)M - M.$$
And then after the third month, the same process happens: accumulate interest on the outstanding balance, then subtract the payment $M$:
$$(1+r)((1+r)^2 P - (1+r)M - M) - M = (1+r)^3 P - (1+r)^2 M - (1+r) M - M.$$
So after $n$ payments, we get
$$(1+r)^n P - (1+r)^{n-1} M - (1+r)^{n-2} M - \cdots - (1+r) M - M.$$
And if, after the $n^{\rm th}$ payment, we have no balance left over, this gives us the equation of value that lets us compute what $M$ needs to be in order for the loan to be paid off.
Starting from @python_enthusiast's answer, the equation to be solved for $i$ is
$$\text{PV}=\frac{\text{FV}}{(1+i)^n}+\frac{\text{PMT}}{i}\Bigg[1-\frac 1{(1+i)^n} \Bigg]$$ which does not show any explicit solution but which does not make any problem using a numerical method.
However, we can make good approximations. For conveniency, I shall use $a=\text{PV}$, $b=\text{FV}$ and $c=\text{PMT}$ and I shall take advantage of the fact that $i \ll 1$.
Using Taylor expansion for the rhs and series reversion, we have
$$i=t+\frac {(3 b+2 c)+3 n (b+c)+c n^2 } { 3(2 b+ c)+3 c n} t^2+O(t^3)$$
where $t=2\frac{(b-a)+c n}{ (2 b+c)n+c n^2 }$
Let me try for $a=2000$, $b=1000$, $c=100$ and $n=15$. This would give
$$i=\frac{823}{39366}=0.0209064$$ while the "exact" solution is $i=0.0213729$.
If this is not sufficiently good, add the next term which is
$$\frac {(n+1)\left(\left(24 b^2+36 b c+14 c^2\right)+n \left(48 b^2+78 b c+31 c^2\right)+n^2 \left(30
b c+22 c^2\right)+5 c^2 n^3 \right)}{36[(2 b+c)n+c n^2]^2}\,t^3$$ This would give
$$i=\frac{1221967}{57395628}=0.0212902$$
Edit
Another formulation
$$i=\frac{-6 ((a-b) (2 b+c))+6 c (-a+3 b+c)n+6 c^2 n^2 } {2 (a-b) (3 b+2 c)+ \left(6 b (a+b)+6 a c-c^2\right)n+2 c (a+2 b)n^2+c^2
n^3 }$$ gives, for the worked example
$$i=\frac{27}{1270}=0.0212598$$
Best Answer
Amount borrowed = Principal = P (in dollar)..
Interest Rate = R% p.a.
Total number of Installments = N. [If the loan is borrowed for n years and the compounding period is semi-annually, then N = 2n.]
Fixed Repayment per Installment = S (in dollar)
To make all later calculations easier, we let X = 1 + R/200 [because the compounding period is semi-annually.].
Try $S = PX^N \dfrac {(X – 1 )}{(X^N – 1)}$
Just re-arrange the above to make P as the subject.