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
回答(1 个)
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 个评论
Matt J
2013-9-11
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
2013-9-11
Yes, all of that was clear. My answer remains unchanged, though. It never relied on the assumption that epsilon and c were equispaced.
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
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
Gerpes
2013-10-30
Matt J
2013-10-30
What does T(x0) return?
Gerpes
2013-10-30
Alan Weiss
2013-10-30
This might not help you much, but I noticed the following poor coding practices:
- Don't call optimset more than once. It is not a very fast function; move the call outside the loop.
- Don't set TolFun to a value less than 1e-14 or so; see the documentation.
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation
类别
在 帮助中心 和 File Exchange 中查找有关 Mathematics 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!