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 Kommentare

sixwwwwww
sixwwwwww am 11 Okt. 2013
Hi Nikhil, you are getting two values of Kn in your first code(trial). Which value you want to pass to second function(loaddistr)?
Nikhil
Nikhil am 11 Okt. 2013
Hey, Yes. I want to transfer Kn values from trial.m to loaddistr.m.
sixwwwwww
sixwwwwww am 11 Okt. 2013
For Kn you get these two values 3.2307e5 and 3.5893e5. So you want to pass both values to loaddistr.m?
Nikhil
Nikhil am 11 Okt. 2013
Yes and I also want to solve for loaddistr.
sixwwwwww
sixwwwwww am 11 Okt. 2013
Bearbeitet: sixwwwwww am 11 Okt. 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
Nikhil
Nikhil am 11 Okt. 2013
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.
Nikhil
Nikhil am 11 Okt. 2013
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.
sixwwwwww
sixwwwwww am 11 Okt. 2013
wait i do it for you. Surely you can't define function like this. I do it wait
Nikhil
Nikhil am 11 Okt. 2013
okay.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

sixwwwwww
sixwwwwww am 11 Okt. 2013

0 Stimmen

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 Kommentare

Nikhil
Nikhil am 11 Okt. 2013
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.
sixwwwwww
sixwwwwww am 11 Okt. 2013
ok I come to it
Nikhil
Nikhil am 11 Okt. 2013
the real problem is how to solve for system of equations while importing values from other function
sixwwwwww
sixwwwwww am 11 Okt. 2013
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?
Nikhil
Nikhil am 11 Okt. 2013
let me try this
sixwwwwww
sixwwwwww am 11 Okt. 2013
ok
Nikhil
Nikhil am 11 Okt. 2013
now the problem is that. Kn itself is the vector. So we will have problem in extracting its value in loaddistr.m
Nikhil
Nikhil am 11 Okt. 2013
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.
sixwwwwww
sixwwwwww am 11 Okt. 2013
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
sixwwwwww
sixwwwwww am 11 Okt. 2013
now you can test
Nikhil
Nikhil am 11 Okt. 2013
??? 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.
sixwwwwww
sixwwwwww am 11 Okt. 2013
But I am not getting any error. Are you using codes I just posted above?
Nikhil
Nikhil am 11 Okt. 2013
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?
sixwwwwww
sixwwwwww am 11 Okt. 2013
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
Nikhil
Nikhil am 11 Okt. 2013
Haha. I will work that out. Anyways thanks a lot for your help.
sixwwwwww
sixwwwwww am 11 Okt. 2013
You are always welcome. If you have further problems you can share as well. Good luck!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Mathematics finden Sie in Hilfe-Center und File Exchange

Tags

Gefragt:

am 10 Okt. 2013

Kommentiert:

am 11 Okt. 2013

Community Treasure Hunt

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

Start Hunting!

Translated by