help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Code question


From: Brett Green
Subject: Re: Code question
Date: Tue, 28 Apr 2020 17:54:39 -0400



On Tue, Apr 28, 2020 at 5:37 PM 이태훈 <address@hidden> wrote:
Thank’s a lot.
I have saved both functions as script and run the code but didn’t worked.
Below are the errors for the codes.
I don’t know how to define the parameters. Isn’t it enough to write just umax=0.08 or I0=1:??
And please tell me If there are any other problem.
Thank you 


>> mu_tilde

warning: function 'mu_tilde' defined within script file '/Users/taehoonlee/mu_tilde.m'
error: 'umax' undefined near line 23 column 29
error: called from
    mu_tilde at line 23 column 23
>> IbX

error: 'I0' undefined near line 27 column 21
error: called from
    IbX>@<anonymous> at line 27 column 23
    integral at line 114 column 7
    IbX at line 28 column

Code
Fn1:
a=3;
b=3;
r=0.0984;
X=9.8;
I0=1;

function IbX_return_value = IbX(IbthetaX,theta)
IbthetaX = @(theta) I0.*exp( -a.*X.*( b.*cos(theta).+sqrt(r.^2-b.^2.*sin(theta).^2) ) );
IbX_return_value = (1/pi).*integral(IbthetaX,0,pi);
end

Fn2:
b=3;
umax=3;
r=0.0983;

function mu_tilde_return_value = mu_tilde(b)
mu_tilde_integrand = @(b) b.*(IbX./(IbX.+KM));
mu_tilde_return_value = (2.*umax./r.^2).*integral(mu_tilde_integrand,0,r);
end



There is a concept in programming known as "scope", which means that a function can only see the variables that are either defined in the function or passed in as arguments. You defined the values outside of the functions.

Also, you deleted the arguments of IbX in the second function, and have done some strange things with replacing variable names with functions.

When the following code is put into the command window

function IbX_return_value = IbX(IbthetaX,theta)
IbthetaX = @(theta) I0.*exp( -a.*X.*( b.*cos(theta).+sqrt(r.^2-b.^2.*sin(theta).^2) ) );
IbX_return_value = (1/pi).*integral(IbthetaX,0,pi);
end

it only defines the function. Later, you can call the function with the desired input variables to evaluate it. Fore example,

function my_return_val = my_sine_function(theta)
my_return_val = sin(theta);
end

just defines a function that returns the sine of its argument. Later I can call it:

>> my_sine_function(pi/2)
ans =  1

I highly recommend reading a basic tutorial on programming before trying to work on the problem you are asking about now. This mailing list is meant to help with occasional problems in Octave, not teach the many fundamental programming concepts you need to learn. Octave and MATLAB use the same language, and there are many good MATLAB tutorials, so I recommend reading some Octave or MATLAB tutorials on how to define functions, what variable scope is, and things like that.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]