solve the equation with parameters and values

I need to solve:
(1 + epsilon*x)(1+x)^(1+epsilon)=c
where epsilon takes 5000 distinct values between -400 and -1/2 and c 30 distinct values between -2 and 2. I have specific values for the epsilon and c pairs, they come from regression estimates. So if there is a function allowing to attach those values to epsilon and c parameters is good.
I need 5000*30 solutions for each pair of epsilon and c.
How to do it?
Thanks

Réponses (1)

Matt J
Matt J le 11 Sep 2013
Modifié(e) : Matt J le 11 Sep 2013

0 votes

You would apply the FZERO command to each epsilon,c pair using a 5000x30 for-loop. Should be pretty fast, assuming you have a good initial guess of the solutions.
Since your epsilon,c values are adjacent to each other, you could probably use the solution for one pair as the initial guess for the next pair.

11 commentaires

Matt J
Matt J le 11 Sep 2013
Gerpes Commented:
Maybe I expressed my self in an unclear way. I have values for epsilon and c, they are not at a constant interval, they come from estimates and are within the values i said above bt not a constant intervals.
What i need is a loop to solve the equation for these 5000*30 combinations of my c and epsilons.
for example in one case I have epsilon=14.3 and c=0.5, if I plug in these values in the equation I find the result of x=0.022762
i would like to know a loop or a procedure that allow me to use these 5000*30 combinations of data from an excel file and ask matlab to substitute them in the equation and spit out 5000*30 xs as a result of these substitutions.
was it clear?
Matt J
Matt J le 11 Sep 2013
Yes, all of that was clear. My answer remains unchanged, though. It never relied on the assumption that epsilon and c were equispaced.
Gerpes
Gerpes le 11 Sep 2013
Modifié(e) : Matt J le 11 Sep 2013
could you please give me a flavor of the code I should write then, by using the previous solution as initial value for he next?
Thanks Matt!
Matt J
Matt J le 11 Sep 2013
Modifié(e) : Matt J le 11 Sep 2013
Something like this maybe
epsilon=sort(epsilon)
c=sort(c);
E=length(epsilon);
C=length(c);
X=zeros(E,C); %solutions
xinitial=...; %as good a guess as you can make
for j=1:C
for i=1:E
epsi=epsilon(i);
cj=c(j);
fun =@(x) (1 + epsi*x)(1+x)^(1+epsi)-cj;
X(i,j)=fzero(fun,xinitial);
xinitial=X(i,j); %assumes neighboring X(i,j) close to each other
if i==1, xinitialOuter=xinitial; end
end
xinitial=xinitialOuter;
end
Gerpes
Gerpes le 12 Sep 2013
Modifié(e) : Matt J le 12 Sep 2013
Thanks Matt,
however I cannot use sort. I just got the data in excel an dimportet them to matlab.
I have 245760 pair-wise data for c and epsilon coming from regressions.
I should apply the above formula to these pairs and get x for every pair. What I did is this:
filename = 'one.xlsx';
c = xlsread(filename,'B:B');
filename = 'two.xlsx';
e = xlsread(filename,'A:A');
sol=zeros(245760,1); %245760 total pairs
x0=-4.3 %initial guess for the first pair computed easily, the solution for the first pair of data has more decimals
for i = 1:245760;
T = @(x) (1+e(i)*x)*(1+x)^(1+e(i))-c(i);
sol (i) = fsolve(T,x0);
options = optimset('TolFun',1e-20); % Option to widen tolerance
end
The issue I get is that at the 1890th pair Matlab gets lost and stops saying:
Error using trustnleqn (line 28) Objective function is returning undefined values at initial point. FSOLVE cannot continue.
Error in fsolve (line 403) [x,FVAL,JACOB,EXITFLAG,OUTPUT,msgData]=...
Now if I try to use the initial value of the prvious iteration point it gets lost even before at the 200th pair.
I don't know why he gives that message since almost all solutions for pairs prior to the 1800th have an imaginary component and he should have stopped way before.
any clue?
Matt J
Matt J le 12 Sep 2013
Modifié(e) : Matt J le 12 Sep 2013
If you're getting imaginary results, it must mean that the solver is testing points where 1+x<0. It might be worth reparametrizing as follows,
T = @(z) (1+e(i)*(z^2-1))*(z^2)^(1+e(i))-c(i);
i.e, with x=z^2-1
Thanks Matt,
that was helpful. However the problem slightly changed now and the T function now has the as the last term e^c(i), which is:
T = @(x) (1+e(i)*x)*(1+x)^(1+e(i))-exp(c(i));
I used the following code:
for i = 1:245760;
T = @(x) (1+e(i)*x)*(1+x)^(1+e(i))-exp(c(i));
Sol(i) = fsolve(T,x0);
options = optimset('TolFun',1e-90);
disp(sol(i))
end
And I get the following error:
Error using trustnleqn (line 28) Objective function is returning undefined values at initial point. FSOLVE cannot continue.
Error in fsolve (line 403) [x,FVAL,JACOB,EXITFLAGOUTPUT,msgData]=...
Do you have any guess on what's going on, how to correct this and move forward? thanks
Matt J
Matt J le 30 Oct 2013
What does T(x0) return?
Gerpes
Gerpes le 30 Oct 2013
It returns sensible values, real numbers. But it gets stuck after the 250th solution it step because there is a jump in the c(i) series from 0 to 133 and gives me back the error message.
Alan Weiss
Alan Weiss le 30 Oct 2013
This might not help you much, but I noticed the following poor coding practices:
  1. Don't call optimset more than once. It is not a very fast function; move the call outside the loop.
  2. Don't set TolFun to a value less than 1e-14 or so; see the documentation.
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation
Matt J
Matt J le 31 Oct 2013
Modifié(e) : Matt J le 31 Oct 2013
Attach a .mat file with the e(i) and c(i) data so that we can run the code.

Connectez-vous pour commenter.

Produits

Question posée :

le 11 Sep 2013

Modifié(e) :

le 31 Oct 2013

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by