Sandeepan Roy
Sandeepan Roy

Reputation: 13

Unable to code non linear equation in MATLAB R2013a - MATLAB giving warning message

I wanted to solve the following equation in MATLAB R2013a using the Symbolic Math Toolbox.

(y/x)-(((1+r)^n)-1)/r=0  where y,x and n>3 are given and r is the dependent variable

I tried myself & coded as follows:

f=solve('(y/x)-(((1+r)^n)-1)/r','r')

but as the solution for r is not exact i.e. it is converging on successive iterations hence MATLAB is giving a warning output with the message

Warning: Explicit solution could not be found.
 f =

[ empty sym ]

How do I code this?

Upvotes: 0

Views: 395

Answers (2)

horchler
horchler

Reputation: 18484

There are an infinite number of solutions to this for an unspecified value of n > 3 and unknown r. I hope that it's pretty clear why – it's effectively asking for a greater and greater number of roots of (1+r)^n. You can find solutions for fixed values of n, however. Note that as n becomes larger there are more and more solutions and of course some of them are complex. I'm going to assume that you're only interested in real values of r. You can use solve and symbolic math for n = 4, n = 5, and n = 6 (for n = 6, the solution may not be in a convenient form):

y = 441361;
x = 66990;
n = 5;
syms r;
rsol = solve(y/x-((1+r)^n-1)/r==0,r,'IgnoreAnalyticConstraints',true)
double(rsol)

However, the question is "do you need all the solutions or just a particular solution for a given value of n"? If you just need a particular solution, you shouldn't be using symbolic math at all as it's slower and has practical issues like the ones you're experiencing. You can instead just use a numerical approach to find a zero of the equation that is near a specified initial guess. fzero is the standard function for solving this sort of problem in a single variable:

y = 441361;
x = 66990;
n = 5;
f = @(r)y/x-((1+r).^n-1)./r;
r0 = 1;
rsol = fzero(f,r0)

You'll see that the value returned is the same as one of the solutions from the symbolic solution above. If you adjust the initial guess r0 (say r0 = -3), it will return the other solution. When using numeric approaches in cases when there are multiple solutions, if you want specific solutions you'll need to know about the behavior of your function and you'll need to add some clever extra code to choose initial guesses.

Upvotes: 0

Iris
Iris

Reputation: 41

I think you forgot to define n as well.

     f=solve('(y/x)-(((1+r)^n)-1)/r=0','n-3>0','r','n')

Should solve your problem :)

Upvotes: 0

Related Questions