How to link two functions

Hello all,
I am want to link following two programs: In first program(trial.m), I am calling fsolve function to solve for system of non linear equations which is stored in second function loaddistr. The two functions trial.m and loaddistr.m are as follows:
%trial curvesumi=0.2017; curvesumo=0.1377; distar=0.6031; dostar=0.6577; mateprop=1e-4*[0.1011 0.0910];
ki=(2/(curvesumi*distar))^(3/2).*curvesumi./(1.5.*mateprop);
ko=(2/(curvesumo*dostar))^(3/2).*curvesumo./(1.5.*mateprop);
Kn=(1./((1./ki).^(1/1.5)+(1./ko).^(1/1.5))).^1.5;
m0=[0.35,0.15,0.05];
p=fsolve(@loaddistr,m0);
function n = loaddistr( m ) Fr=8900; Pd=0.015; Z=9;
Kn=3.5893e+005;
Jr=[0.1156,0.1590,0.1892,0.2117,0.2288,0.2416,0.2505,0.2559,0.2576,0.2546,0.2289,0.1871,0.1339,0.0711];
eps=[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.25, 1.67, 2.5, 5.0];
n=[m(1)-0.5*(1-0.5*Pd/m(3));
m(3)-Pd/2-(Fr/(Z*Kn*m(2)))^(1/1.5);
m(2)-spline(eps,Jr,m(1))];
end
If you notice I am using Kn term in both programs. I want to transfer this Kn value directly from trial.m to loaddistr.m, without separately specifying it in loaddistr.m So can someone please guide me about the code which will allow me to solve for loaddistr.m by using the Kn value from trial.m.
Thanks in advance,
Nikhil

9 Comments

Hi Nikhil, you are getting two values of Kn in your first code(trial). Which value you want to pass to second function(loaddistr)?
Hey, Yes. I want to transfer Kn values from trial.m to loaddistr.m.
For Kn you get these two values 3.2307e5 and 3.5893e5. So you want to pass both values to loaddistr.m?
Yes and I also want to solve for loaddistr.
sixwwwwww
sixwwwwww on 11 Oct 2013
Edited: sixwwwwww on 11 Oct 2013
Why don't you embed loaddistr.m code within trial.m code. Because calculations are performed sequentially. Also by passing two values of Kn you will not three but four values at the end as a result
Hey, as per your suggestion I tried following thing.. but the I got error as follows curvesumi=0.2017; curvesumo=0.1377; distar=0.6031; dostar=0.6577; mateprop=1e-4*[0.1011 0.0910];
ki=(2/(curvesumi*distar))^(3/2).*curvesumi./(1.5.*mateprop);
ko=(2/(curvesumo*dostar))^(3/2).*curvesumo./(1.5.*mateprop);
Kn=(1./((1./ki).^(1/1.5)+(1./ko).^(1/1.5))).^1.5;
m0=[0.35,0.15,0.05];
p=fsolve(@loaddistr,m0);
function n = loaddistr( m )
Fr=8900; Pd=0.015; Z=9;
%Kn=3.5893e+005;
Jr=[0.1156,0.1590,0.1892,0.2117,0.2288,0.2416,0.2505,0.2559,0.2576,0.2546,0.2289,0.1871,0.1339,0.0711];
eps=[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.25, 1.67, 2.5, 5.0];
n=[m(1)-0.5*(1-0.5*Pd/m(3)); m(3)-Pd/2-(Fr/(Z*Kn*m(2)))^(1/1.5); m(2)-spline(eps,Jr,m(1))];
end
??? function n = loaddistr( m ) | Error: Function definitions are not permitted in this context.
I just copy paste loaddistr.m in trial.m script .. and tried to run it.. but the error I got is
??? function n = loaddistr( m ) | Error: Function definitions are not permitted in this context.
wait i do it for you. Surely you can't define function like this. I do it wait
okay.

Sign in to comment.

 Accepted Answer

Hey here is your merged code:
curvesumi = 0.2017;
curvesumo = 0.1377;
distar = 0.6031;
dostar = 0.6577;
mateprop = 1e-4 * [0.1011 0.0910];
ki = (2 / (curvesumi * distar)) ^ (3 / 2) .* curvesumi ./ (1.5 .* mateprop);
ko = (2 / (curvesumo * dostar)) ^ (3 / 2) .* curvesumo ./ (1.5 .* mateprop);
Kn = (1 ./ ((1 ./ ki) .^ (1 / 1.5) + (1 ./ ko) .^ (1 / 1.5))) .^ 1.5;
m0 = [0.35, 0.15, 0.05];
Fr = 8900;
Pd = 0.015;
Z = 9;
Jr = [0.1156, 0.1590, 0.1892, 0.2117, 0.2288, 0.2416, 0.2505, 0.2559, 0.2576, 0.2546, 0.2289, 0.1871, 0.1339, 0.0711];
eps = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.25, 1.67, 2.5, 5.0];
n = [m0(1) - 0.5 * (1 - 0.5 * Pd / m0(3));
m0(3) - Pd / 2 - (Fr / (Z * Kn(1) * m0(2))) ^ (1 / 1.5); % for first value of Kn
m0(3) - Pd / 2 - (Fr / (Z * Kn(2) * m0(2))) ^ (1 / 1.5); % for second value of Kn
m0(2) - spline(eps, Jr, m0(1))];

16 Comments

Nopes. Your missing crucial point over here. n is non linear system of equations in m. I am trying to solve for this system of equations using fsolve and initial value m0. Kn term is used in one of this equations.
ok I come to it
the real problem is how to solve for system of equations while importing values from other function
We can pass Kn as fourth element in m0 in trial.m and in loaddistr.m we can separate m0 and Kn because there m0 elements are used individually. Do you agree?
let me try this
now the problem is that. Kn itself is the vector. So we will have problem in extracting its value in loaddistr.m
I am getting an error for that. ??? Undefined function or method 'm0' for input arguments of type 'double'.
Error in ==> loaddistr at 6 Kn=m0(4);
Error in ==> fsolve at 248 fuser = feval(funfcn{3},x,varargin{:});
Caused by: Failure in initial user-supplied objective function evaluation. FSOLVE cannot continue.
trial.m will be like this:
%trial
curvesumi=0.2017;
curvesumo=0.1377;
distar=0.6031;
dostar=0.6577;
mateprop=1e-4*[0.1011 0.0910];
ki=(2/(curvesumi*distar))^(3/2).*curvesumi./(1.5.*mateprop);
ko=(2/(curvesumo*dostar))^(3/2).*curvesumo./(1.5.*mateprop);
Kn=(1./((1./ki).^(1/1.5)+(1./ko).^(1/1.5))).^1.5;
m0=[0.35,0.15,0.05, Kn(1)];
p=fsolve(@loaddistr,m0);
and loaddistr.m will be like this:
function n = loaddistr(m)
Fr=8900;
Pd=0.015;
Z=9;
Kn=m(4);
Jr=[0.1156,0.1590,0.1892,0.2117,0.2288,0.2416,0.2505,0.2559,0.2576,0.2546,0.2289,0.1871,0.1339,0.0711];
eps=[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.25, 1.67, 2.5, 5.0];
n=[m(1)-0.5*(1-0.5*Pd/m(3));
m(3)-Pd/2-(Fr/(Z*Kn*m(2)))^(1/1.5);
m(2)-spline(eps,Jr,m(1))];
end
now you can test
??? Undefined function or method 'm0' for input arguments of type 'double'.
Error in ==> loaddistr at 6 Kn1=m0(4);
Error in ==> fsolve at 248 fuser = feval(funfcn{3},x,varargin{:});
Caused by: Failure in initial user-supplied objective function evaluation. FSOLVE cannot continue.
This is the error I am getting. The problem is that fsolve solves for m. Hence I guess we can not put values Kn value in m0. Whats your opinion.
But I am not getting any error. Are you using codes I just posted above?
Hey, Thanks that worked. There was small typo. But now the issue is that I will have to use for loop to solve loaddistr for each value of Kn(i). right? or is there any efficient way to do it?
The thing is that for each value of Kn you will get different set of equations which you will need to solve. So you will have to use for loop inevitably. Wish you good luck
Haha. I will work that out. Anyways thanks a lot for your help.
You are always welcome. If you have further problems you can share as well. Good luck!

Sign in to comment.

More Answers (0)

Categories

Find more on Mathematics in Help Center and File Exchange

Tags

Asked:

on 10 Oct 2013

Commented:

on 11 Oct 2013

Community Treasure Hunt

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

Start Hunting!