solve nonlinear equations + numerical integration

조회 수: 5 (최근 30일)
john birt
john birt 2012년 4월 25일
Hi, I just have no idea how to do this.. I have code
x = [1.4334 1.46 0.1; 1.435 1.46 0.1];
t = 0.1:0.1:0.6;
for i=1:length(x)
z(i) = trapz(t,normcdf(((log(x(i,1)./x(i,2))+t.*x(i,3).^2)),0,1));
end
But I do not know what x(:,3) is, above I have just used x(1,3)=x(2,3)=0.1 but that is just a guess! I do now what the above function z(i) should be equal to for each 'i', z(1)=0.0213 and z(2)=0.0222 and I want to solve to find a x(1,3)=x(2,3). I do not want to find a single numeric value x(1,3)=x(2,3) that will not solve the equations exactly but just the best x(1,3)=x(2,3) that fits both equations in terms of mean squared error. I do not want two values x(1,3) and x(2,3) that are different, I am not trying to solve each equation individual but a x(.,3) that solves them all best in terms of mse.
Heres my code
x = [1.4334 1.46 0.1; 1.435 1.46 0.1];
y = [0.0213, 0.0222]
t = 0.1:0.1:0.6;
xdata = x(:,1); ydata = y;
for i=1:length(x)
z(i) = trapz(t,normcdf(((log(x(i,1)./x(i,2))+t.*c.^2)),0,1));;
end
c = 0.1; %initial guess for x(:,3)
cfit = nlinfit(xdata,ydata,z,c)
which does not work. I just dont understand how to simultaneously solve a set of equations when there is numerical integration involved.
Any ideas?
  댓글 수: 3
john birt
john birt 2012년 4월 25일
Yes I fully get your point and in answer it is not different from the second link above which is why I deleted that unanswered question.
I've asked this question again but re-formatted the code to try and make it a simpler question that people might understand better and be able to answer.
The post "36536-numerical-integration" does not invlove "simultaneously solving a set of equations" as this question does.
john birt
john birt 2012년 4월 26일
I think you answered my question, but I think I asked the wrong question :-)

댓글을 달려면 로그인하십시오.

채택된 답변

Richard Brown
Richard Brown 2012년 4월 25일
You could pose it as an optimization problem. First set up a function that will be zero when z1,z2 are equal to their correct values. I'm going to define the vector y to contain your two decision variables (x(1,3) and x(2,3))
t = 0.1:0.1:0.6;
z = [0.0213; 0.0222]
x = [1.4334 1.46; 1.435 1.46]; %unknowns ommited
f = @(y) sum(([trapz(t,normcdf(((log(x(1,1)./x(1,2))+t.*y(1).^2)),0,1));
trapz(t,normcdf(((log(x(2,1)./x(2,2))+t.*y(2).^2)),0,1))] - z).^2);
And then use fminsearch to find it:
[y, fval] = fminsearch(f, [0.1; 0.1])
If fval == 0, then y contains a solution to your equations. Disclaimer: I don't have the stats toolbox on my computer, so I can't run this, but with a bit of luck (and a good enough initial guess), it will hopefully work.
  댓글 수: 3
john birt
john birt 2012년 4월 26일
The code runs and works but sadly does not give the required result as it produces a y(1) and a y(2), and I am only looking for a single 'y' that fits both equations the best it can, "best" in terms of mean squared error.
john birt
john birt 2012년 4월 26일
I'm going to have to delete this question a re-post it in a better way to get people to understand what my question is.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Walter Roberson
Walter Roberson 2012년 4월 25일
Looks to me like this code would be appropriate:
lnx12 = log(x(:,1) ./ x(:,2));
cguess = 0.1;
C = zeros(1,length(x));
for i=1:length(x)
C(i) = fzero( @(c) z(i) - trapz(t, normcdf(lnx12(i)+t.*c.^2), 0, 1), cguess);
end
cfit = nlinfit(xdata,ydata,z,C);
  댓글 수: 2
john birt
john birt 2012년 4월 25일
thanks I will try this
john birt
john birt 2012년 4월 26일
thanks, It was 50:50 whose answer I accepted, I would have liked to be able to click accepted yours as well :-) Anyhow, I asked the wrong question. My code is huge and I have tried to ask a simple question but asked the wrong thing, so I've put up a new question trying to make it really simple.

댓글을 달려면 로그인하십시오.


Richard Brown
Richard Brown 2012년 4월 25일
Final answer: your equations have no solution
t = 0.1:0.1:0.6;
z = [0.0213; 0.0222];
x = [1.4334 1.46; 1.435 1.46];
f1 = @(y) trapz(t, normcdf(log(x(1,1)/x(1,2) + t*y^2),0,1))-z(1)
f2 = @(y) trapz(t, normcdf(log(x(2,1)/x(2,2) + t*y^2),0,1))-z(2)
Now plot both functions - neither has a zero
fplot(f1, [-10, 10])
hold on
fplot(f2, [-10, 10])
  댓글 수: 3
Richard Brown
Richard Brown 2012년 4월 25일
Cool - use |fzero| individually for each equation from *this* example, not my previous answer
john birt
john birt 2012년 4월 26일
thank you again. Back at my desk now so will try shortly, the issue that I did not make clear is that I am not trying to find a 'c' that solves each equation exactly, but just a single 'c' that fits both equations the best it can, they will not solve these equations but give the best 'c' in terms of mean squared error.
Anyhow, than kyou for your advice and I will have another try today to get some results.

댓글을 달려면 로그인하십시오.

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by