Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Making two functions equal

Subject: Making two functions equal

From: Reshma B

Date: 3 Jul, 2011 20:20:25

Message: 1 of 8

Hi Sir,

I have two functions. In the first function I input the value of K1 and I get W1. In the second function I input the value of K3 and I get W3 values. Now I want to make W1 and W3 values to be equal just by varying K1 value. When I does it manually I get a value of K1 with 8 decimal points. Below is my code. But it is taking a long time to run. Is there any easier method to do this?

K=0.01;
S1=r_expmnt_equi_2(K);
S3=r_expmnt_equi( );

while S1>S3
    K=K+0.01;
    S1=r_expmnt_equi_2(K);
end

while S3>S1 && abs(S3-S1)>0.0000001
    K=K-0.0000001;
    S1=r_expmnt_equi_2(K);
end
 
Hoping to see the replies

Subject: Making two functions equal

From: Roger Stafford

Date: 3 Jul, 2011 21:05:10

Message: 2 of 8

"Reshma B" wrote in message <iuqiu9$mdj$1@newscl01ah.mathworks.com>...
> Hi Sir,
>
> I have two functions. In the first function I input the value of K1 and I get W1. In the second function I input the value of K3 and I get W3 values. Now I want to make W1 and W3 values to be equal just by varying K1 value. When I does it manually I get a value of K1 with 8 decimal points. Below is my code. But it is taking a long time to run. Is there any easier method to do this?
>
> K=0.01;
> S1=r_expmnt_equi_2(K);
> S3=r_expmnt_equi( );
>
> while S1>S3
> K=K+0.01;
> S1=r_expmnt_equi_2(K);
> end
>
> while S3>S1 && abs(S3-S1)>0.0000001
> K=K-0.0000001;
> S1=r_expmnt_equi_2(K);
> end
>
> Hoping to see the replies
- - - - - - - - - - -
  The code you exhibit depends on the function r_expmnt_equi_2 being monotonically decreasing - when K increases, then r_expmnt_equi_2(K) decreases. This being the case, you can increase efficiency by using a binary search technique.

  Let K1 < K2 be so extreme that it is known in advance that

 r_expmnt_equi_2(K1) > S3 and r_expmnt_equi_2(K2) < S3

K3 = (K1+K2)/2;
S1 = r_expmnt_equi_2(K3);
b = (abs(S3-S1) > tol);
while b
 if S1 < S3, K2 = K3; else, K1 = K3; end
 K3 = (K1+K2)/2;
 S1 = r_expmnt_equi_2(K3);
 b = (abs(S3-S1) > tol);
end

where 'tol' is some suitably small value. Then K3 is the desired value.

Roger Stafford

Subject: Making two functions equal

From: Roger Stafford

Date: 3 Jul, 2011 21:57:08

Message: 3 of 8

"Reshma B" wrote in message <iuqiu9$mdj$1@newscl01ah.mathworks.com>...
> Hi Sir,
>
> I have two functions. In the first function I input the value of K1 and I get W1. In the second function I input the value of K3 and I get W3 values. Now I want to make W1 and W3 values to be equal just by varying K1 value. When I does it manually I get a value of K1 with 8 decimal points. Below is my code. But it is taking a long time to run. Is there any easier method to do this?
>
> K=0.01;
> S1=r_expmnt_equi_2(K);
> S3=r_expmnt_equi( );
>
> while S1>S3
> K=K+0.01;
> S1=r_expmnt_equi_2(K);
> end
>
> while S3>S1 && abs(S3-S1)>0.0000001
> K=K-0.0000001;
> S1=r_expmnt_equi_2(K);
> end
>
> Hoping to see the replies
- - - - - - - -
  On second thought, never mind the code I sent. Just use 'fzero'. It undoubtedly uses binary searching itself but is more robust.

Roger Stafford

Subject: Making two functions equal

From: Reshma B

Date: 5 Jul, 2011 20:30:25

Message: 4 of 8

Dear Sir,

Thank you very much for your reply.
I tried to solve the problem using fzero. I am not familiar with using fzero. Could you please tell me the problem?
q=input('Enter q');
Ke=0.01;
S3=r_nl_3layers_noG_equi_fn_noinput(q);
S1 = r_nonlinear_1layer_noG_equi_fn_noinput(q,Ke);
b = abs(S3-S1);
Ke=fzero('b',Ke,optimset('TolX',1e-15));



"Roger Stafford" wrote in message <iuqojk$69r$1@newscl01ah.mathworks.com>...
> "Reshma B" wrote in message <iuqiu9$mdj$1@newscl01ah.mathworks.com>...
> > Hi Sir,
> >
> > I have two functions. In the first function I input the value of K1 and I get W1. In the second function I input the value of K3 and I get W3 values. Now I want to make W1 and W3 values to be equal just by varying K1 value. When I does it manually I get a value of K1 with 8 decimal points. Below is my code. But it is taking a long time to run. Is there any easier method to do this?
> >
> > K=0.01;
> > S1=r_expmnt_equi_2(K);
> > S3=r_expmnt_equi( );
> >
> > while S1>S3
> > K=K+0.01;
> > S1=r_expmnt_equi_2(K);
> > end
> >
> > while S3>S1 && abs(S3-S1)>0.0000001
> > K=K-0.0000001;
> > S1=r_expmnt_equi_2(K);
> > end
> >
> > Hoping to see the replies
> - - - - - - - -
> On second thought, never mind the code I sent. Just use 'fzero'. It undoubtedly uses binary searching itself but is more robust.
>
> Roger Stafford

Subject: Making two functions equal

From: Steven_Lord

Date: 5 Jul, 2011 21:45:06

Message: 5 of 8



"Reshma B" <rbmindworking@gmail.com> wrote in message
news:iuvs91$19b$1@newscl01ah.mathworks.com...
> Dear Sir,
>
> Thank you very much for your reply. I tried to solve the problem using
> fzero. I am not familiar with using fzero. Could you please tell me the
> problem?
> q=input('Enter q');
> Ke=0.01;
> S3=r_nl_3layers_noG_equi_fn_noinput(q);
> S1 = r_nonlinear_1layer_noG_equi_fn_noinput(q,Ke); b = abs(S3-S1);
> Ke=fzero('b',Ke,optimset('TolX',1e-15));

Create a function that accepts an input, evaluates your two functions for
that input, and returns their difference. Then call FZERO on _that_
function.


% Begin subtractTheTwoFunctions.m
function thedifference = subtractTheTwoFunctions(q, Ke)
valueOfFirstFunction = r_nl_3layers_noG_equi_fn_noinput(q);
valueOfSecondFunction = r_nonlinear_1layer_noG_equi_fn_noinput(q,Ke);
thedifference = valueOfFirstFunction -valueOfSecondFunction;
% End subtractTheTwoFunctions.m


% Paste the following into the Command Window once you've created the
function above
Ke = 0.01;
initialGuess = 0.5;
options = optimset('TolX', 1e-8);
parameterValue = fzero(@(p) subtractTheTwoFunctions(p, Ke), initialGuess,
options);

% To check, compute the value of each function for the parameterValue
returned by FZERO:
valueOfFirstAtParameter = r_nl_3layers_noG_equi_fn_noinput(parameterValue);
valueOfSecondAtParameter =
r_nonlinear_1layer_noG_equi_fn_noinput(parameterValue, 0.01);

% Now subtract the function values. The difference should be small.
differenceAtParameter = valueOfFirstAtParameter-valueOfSecondAtParameter


--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Making two functions equal

From: Reshma B

Date: 6 Jul, 2011 06:33:10

Message: 6 of 8

Dear Sir,

Thanks for your reply. I am getting a NAN value when run the programme. But I am getting the answer for the same input when I did using bisection method. But it is consuming much of my time. Is there any way to solve this? my input was q=0.2;
Regards,
Reshma
"Steven_Lord" <slord@mathworks.com> wrote in message <iv00l3$d2b$1@newscl01ah.mathworks.com>...
>
>
> "Reshma B" <rbmindworking@gmail.com> wrote in message
> news:iuvs91$19b$1@newscl01ah.mathworks.com...
> > Dear Sir,
> >
> > Thank you very much for your reply. I tried to solve the problem using
> > fzero. I am not familiar with using fzero. Could you please tell me the
> > problem?
> > q=input('Enter q');
> > Ke=0.01;
> > S3=r_nl_3layers_noG_equi_fn_noinput(q);
> > S1 = r_nonlinear_1layer_noG_equi_fn_noinput(q,Ke); b = abs(S3-S1);
> > Ke=fzero('b',Ke,optimset('TolX',1e-15));
>
> Create a function that accepts an input, evaluates your two functions for
> that input, and returns their difference. Then call FZERO on _that_
> function.
>
>
> % Begin subtractTheTwoFunctions.m
> function thedifference = subtractTheTwoFunctions(q, Ke)
> valueOfFirstFunction = r_nl_3layers_noG_equi_fn_noinput(q);
> valueOfSecondFunction = r_nonlinear_1layer_noG_equi_fn_noinput(q,Ke);
> thedifference = valueOfFirstFunction -valueOfSecondFunction;
> % End subtractTheTwoFunctions.m
>
>
> % Paste the following into the Command Window once you've created the
> function above
> Ke = 0.01;
> initialGuess = 0.5;
> options = optimset('TolX', 1e-8);
> parameterValue = fzero(@(p) subtractTheTwoFunctions(p, Ke), initialGuess,
> options);
>
> % To check, compute the value of each function for the parameterValue
> returned by FZERO:
> valueOfFirstAtParameter = r_nl_3layers_noG_equi_fn_noinput(parameterValue);
> valueOfSecondAtParameter =
> r_nonlinear_1layer_noG_equi_fn_noinput(parameterValue, 0.01);
>
> % Now subtract the function values. The difference should be small.
> differenceAtParameter = valueOfFirstAtParameter-valueOfSecondAtParameter
>
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

Subject: Making two functions equal

From: Reshma B

Date: 6 Jul, 2011 10:13:10

Message: 7 of 8

"Reshma B" wrote in message <iv0vj6$rlf$1@newscl01ah.mathworks.com>...
> Dear Sir,
> Thank you very much for your reply.I got the values. But when I enter some values of q, I am getting an error that

Warning: Matrix is close to singular or badly scaled.
         Results may be inaccurate. RCOND = 4.058524e-017.

Is there any way to correct it?
 Regards,
 Reshma

> "Steven_Lord" <slord@mathworks.com> wrote in message <iv00l3$d2b$1@newscl01ah.mathworks.com>...
> >
> >
> > "Reshma B" <rbmindworking@gmail.com> wrote in message
> > news:iuvs91$19b$1@newscl01ah.mathworks.com...
> > > Dear Sir,
> > >
> > > Thank you very much for your reply. I tried to solve the problem using
> > > fzero. I am not familiar with using fzero. Could you please tell me the
> > > problem?
> > > q=input('Enter q');
> > > Ke=0.01;
> > > S3=r_nl_3layers_noG_equi_fn_noinput(q);
> > > S1 = r_nonlinear_1layer_noG_equi_fn_noinput(q,Ke); b = abs(S3-S1);
> > > Ke=fzero('b',Ke,optimset('TolX',1e-15));
> >
> > Create a function that accepts an input, evaluates your two functions for
> > that input, and returns their difference. Then call FZERO on _that_
> > function.
> >
> >
> > % Begin subtractTheTwoFunctions.m
> > function thedifference = subtractTheTwoFunctions(q, Ke)
> > valueOfFirstFunction = r_nl_3layers_noG_equi_fn_noinput(q);
> > valueOfSecondFunction = r_nonlinear_1layer_noG_equi_fn_noinput(q,Ke);
> > thedifference = valueOfFirstFunction -valueOfSecondFunction;
> > % End subtractTheTwoFunctions.m
> >
> >
> > % Paste the following into the Command Window once you've created the
> > function above
> > Ke = 0.01;
> > initialGuess = 0.5;
> > options = optimset('TolX', 1e-8);
> > parameterValue = fzero(@(p) subtractTheTwoFunctions(p, Ke), initialGuess,
> > options);
> >
> > % To check, compute the value of each function for the parameterValue
> > returned by FZERO:
> > valueOfFirstAtParameter = r_nl_3layers_noG_equi_fn_noinput(parameterValue);
> > valueOfSecondAtParameter =
> > r_nonlinear_1layer_noG_equi_fn_noinput(parameterValue, 0.01);
> >
> > % Now subtract the function values. The difference should be small.
> > differenceAtParameter = valueOfFirstAtParameter-valueOfSecondAtParameter
> >
> >
> > --
> > Steve Lord
> > slord@mathworks.com
> > To contact Technical Support use the Contact Us link on
> > http://www.mathworks.com

Subject: Making two functions equal

From: Steven_Lord

Date: 6 Jul, 2011 13:41:17

Message: 8 of 8



"Reshma B" <rbmindworking@gmail.com> wrote in message
news:iv0vj6$rlf$1@newscl01ah.mathworks.com...
> Dear Sir,
>
> Thanks for your reply. I am getting a NAN value when run the programme.
> But I am getting the answer for the same input when I did using bisection
> method. But it is consuming much of my time. Is there any way to solve
> this? my input was q=0.2;

Impossible to say without seeing the code for your two functions. Please
post them to the newsgroup (do NOT email them to me directly) and someone
may be able to offer suggestions as to what's going wrong.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us