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:
how import a variable from script file to function file?

Subject: how import a variable from script file to function file?

From: ghasem

Date: 15 May, 2013 07:21:07

Message: 1 of 4

Hi.
I have to solve a complex non-linear equation.therefore,I have two equation two unknown.
I have to solve my equation in a loop for,repeatedly(for different initial guess points).
therefore I wrote my equation in a function file and saved it.
NOW,I created a script file and I want to run this script file to run function file,for each guess point.
now my problem is that,I want to use from loop's index in function file,aslo.but I don't know how do it.
for example,my script file is:
for i =1:100
real_guess(i) = i/100;
imag_guess(i) = i/500;
my_guess = [ real_guess(i) , imag_guess(i)]
equ1 = fsolve(@my_function_name , my_guess);
...
end
I created a function as "my_function_name.m" and saved it in current directory.
Now,when call "my_function_name" in the above with fslove,I want to use from "i" index,in my_function_name program,also.
how do it?
thanks
ghasem

Subject: how import a variable from script file to function file?

From: ghasem

Date: 15 May, 2013 12:38:07

Message: 2 of 4

is there anyone who help me?
How import a "i" index from script to function?
I need to import "i" index from script to function,where function include a two equation two variable.
for example my function is as following:
%
function output = func_bedune_dielec(inputs)
realPartOfInput = inputs(1);
imagPartOfInput = inputs(2);
% k1 , k2 , are known.and w is a vector that length(w)=100.
% namely : w =linspace(1,5,100);
kz = complex(realPartOfInput, imagPartOfInput);
er = 1-w(i)^2;
k2 = k1*sqrt(er);
g1 = er*sqrt(kz^2-k3^2).*besseli(1,sqrt(-kz^2-k2^2)).*besselk(0,sqrt(-kz^2-k3^2))+... sqrt(-kz^2-k2^2).*besseli(0,sqrt(-kz^2-k2^2)).*besselk(1,sqrt(-kz^2-k3^2));
output = [ real(g1); imag(g1)];

%
in fact,when function in FSOLVE is called,I want to use from w(i) in my function,so that "i" is for loop index of my script.
how import "i" index value from script to function,when function is called?
ghasem

Subject: how import a variable from script file to function file?

From: Steven_Lord

Date: 15 May, 2013 14:33:27

Message: 3 of 4



"ghasem " <shaban_sadeghi@yahoo.com> wrote in message
news:kmvd13$jrv$1@newscl01ah.mathworks.com...
> Hi.
> I have to solve a complex non-linear equation.therefore,I have two
> equation two unknown.
> I have to solve my equation in a loop for,repeatedly(for different initial
> guess points).
> therefore I wrote my equation in a function file and saved it.
> NOW,I created a script file and I want to run this script file to run
> function file,for each guess point.
> now my problem is that,I want to use from loop's index in function
> file,aslo.but I don't know how do it.
> for example,my script file is:
> for i =1:100
> real_guess(i) = i/100;
> imag_guess(i) = i/500;
> my_guess = [ real_guess(i) , imag_guess(i)]
> equ1 = fsolve(@my_function_name , my_guess);
> ...
> end
> I created a function as "my_function_name.m" and saved it in current
> directory.
> Now,when call "my_function_name" in the above with fslove,I want to use
> from "i" index,in my_function_name program,also.
> how do it?

See the Note box in the Description section of the documentation page for
FSOLVE for a description of different ways to pass extra parameters into the
function that you specify as the first input argument to FSOLVE.

http://www.mathworks.com/help/optim/ug/fsolve.html

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

Subject: how import a variable from script file to function file?

From: ghasem

Date: 15 May, 2013 16:31:10

Message: 4 of 4

> See the Note box in the Description section of the documentation page for
> FSOLVE for a description of different ways to pass extra parameters into the
> function that you specify as the first input argument to FSOLVE.
>
> http://www.mathworks.com/help/optim/ug/fsolve.html
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com
===========================
I didn't understand,exactly.namely,I have to add a input argument in FSOLVE that include in "i" index values,when call my_function_name?namely I have to write as following?
equ1 = fsolve(@my_function_name , my_guess,i);
========
but I receive following error:
??? Undefined function or variable 'i'.
Error in ==> my_function_name at 51
    er = 1-w(i)^2;

Error in ==> fsolve at 248
            fuser = feval(funfcn{3},x,varargin{:});

Error in ==> my_script at 26
equ1 = fsolve(@my_func_name,my_guess,i);
=============
again,I write total of my code (function and script) as following:

% first : I wrote function section as following:

function output = my_func_name(inputs)
realPartOfInput = inputs(1);
imagPartOfInput = inputs(2);
% k1 , k2 , are known.and w is a vector that length(w)=100.
% namely : w =linspace(1,5,100);
kz = complex(realPartOfInput, imagPartOfInput);
er = 1-w(i)^2;
k2 = k1*sqrt(er);
g1 = er*sqrt(kz^2-k3^2).*besseli(1,sqrt(-kz^2-k2^2)).*besselk(0,sqrt(-kz^2-k3^2))+... sqrt(-kz^2-k2^2).*besseli(0,sqrt(-kz^2-k2^2)).*besselk(1,sqrt(-kz^2-k3^2));
output = [ real(g1); imag(g1)];

% and saved above function in a directory.
% second: I wrote a script that has a "for loop" for different initial guess:
for i =1:100
real_guess(i) = i/100;
imag_guess(i) = i/500;
my_guess = [ real_guess(i) , imag_guess(i)]
equ1 = fsolve(@my_function_name , my_guess);
...
end
% and saved this script same directory with function path.
====================
NOW,how transfer "i" index value in script to "i" index in "1-w(i)^2" formula within my function file?
please help me...

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