Scilab Error 4 Undefined variable <function_name> - function

hi ppl im new at Scilab,
im doing a script which i send the user through menus and i'm making a function for every sub-menu.
I read that functions can have 0 input parameters but it must have at least 1 output parameter.
According to this i wrote this
//execution file landing menu
option='1'
while (option~=0)
clc
mprintf('1 - First Sub-menu')
mprintf('2 - Second Sub-menu')
option=input('Select the option: ', 's')
select option
case '1' then
result=sub_one(),
case '2' then
result=sub_two(),
else
disp('Invalid Option!!!')
end
end
//Function sub_one
function result=sub_one()
option='1'
while (option~=0)
clc
mprintf('1 - Do stuff')
mprintf('2 - Do other stuff')
option=input('Select the option: ', 's')
select option
case '1' then
result=do_stuff(),
case '2' then
result=do_other_stuff(),
else
disp('Invalid Option!!!')
end
end
result=0
endfunction
and i always get the error
result=sub_one(),
!--error 4
Undefined variable: sub_one
at line xx of exec file called by :
exec('dir\dir\dir\file.sce', -1)
this is freaking annoying me
some experter then me?

Scilab parses the file top to bottom, so when it is in your main part at the top of the file, sub_one does not exist yet. If you switch the order around it will work.
If you would like to retain the order in your file you can also do the following:
// Define some main function
function main()
disp('hello from main')
sub_one()
endfunction
// Define some sub function
function sub_one()
disp('hello from sub_one')
endfunction
// call function main to execute
main()

Related

Rerun the program after it ends automatically in octave

Using OCTAVE only...
How can I rerun the code automatically after it ends. Like I want to make a program in which if the input is incorrect value it will end the program and rerun again.
I tried that by writting there a file name and it works but this will only work until I change my file name.
You can wrap your main script in a wrapper script which performs the loop.
% In main.m
disp( 'Hello from main' );
Question = "Do you want to rerun? ";
Response = input( Question, 's');
% in wrapper.m
Response = 'yes';
while strcmp( Response, 'yes' )
main
end

Why Octave doesn't see this object?

I wanted to code a function in octave, however I found some problems during doing so:
N=700;
T=900;
lambda=N/T;
x=randn(N,T);
s=std(x(:));
r=x*x'/T;
l=eig(r);
lambda_plus=(s^2)*(1+sqrt(lambda))^2;
lambda_minus=(s^2)*(1-sqrt(lambda))^2;
# Define a function - value of this function depenends
# on position of x
function kiki = avg (x)
if (x <= lambda_plus && x >= lambda_minus)
(1/(2*pi*lambda*x*s^(2)))*sqrt((lambda_plus-x)*(x-lambda_minus));
else
0
endif
endfunction
Then I wanted to run this function so I did run avg(2) but it didn't work.
Error I saw was:
error: 'lambda_plus' undefined near line 15, column 15
but it's not true! lambda_plus is defined before the definition of the function!
I read that this problem might be, because octave doesn't see our function and the solution is to:
(1) Save the file with name of the function - in my case avg.m
(2) Open a new file and in new file run your function
I did exactly what they were saying and in newly created file I ran avg(2) but unfortunately with exactly same result.

How do I set a function to a variable in MATLAB

As a homework assignment, I'm writing a code that uses the bisection method to calculate the root of a function with one variable within a range. I created a user function that does the calculations, but one of the inputs of the function is supposed to be "fun" which is supposed to be set equal to the function.
Here is my code, before I go on:
function [ Ts ] = BisectionRoot( fun,a,b,TolMax )
%This function finds the value of Ts by finding the root of a given function within a given range to a given
%tolerance, using the Bisection Method.
Fa = fun(a);
Fb = fun(b);
if Fa * Fb > 0
disp('Error: The function has no roots in between the given bounds')
else
xNS = (a + b)/2;
toli = abs((b-a)/2);
FxNS = fun(xns);
if FxNS == 0
Ts = xNS;
break
end
if toli , TolMax
Ts = xNS;
break
end
if fun(a) * FxNS < 0
b = xNS;
else
a = xNS;
end
end
Ts
end
The input arguments are defined by our teacher, so I can't mess with them. We're supposed to set those variables in the command window before running the function. That way, we can use the program later on for other things. (Even though I think fzero() can be used to do this)
My problem is that I'm not sure how to set fun to something, and then use that in a way that I can do fun(a) or fun(b). In our book they do something they call defining f(x) as an anonymous function. They do this for an example problem:
F = # (x) 8-4.5*(x-sin(x))
But when I try doing that, I get the error, Error: Unexpected MATLAB operator.
If you guys want to try running the program to test your solutions before posting (hopefully my program works!) you can use these variables from an example in the book:
fun = 8 - 4.5*(x - sin(x))
a = 2
b = 3
TolMax = .001
The answer the get in the book for using those is 2.430664.
I'm sure the answer to this is incredibly easy and straightforward, but for some reason, I can't find a way to do it! Thank you for your help.
To get you going, it looks like your example is missing some syntax. Instead of either of these (from your question):
fun = 8 - 4.5*(x - sin(x)) % Missing function handle declaration symbol "#"
F = # (x) 8-4.5*(x-sin9(x)) %Unless you have defined it, there is no function "sin9"
Use
fun = #(x) 8 - 4.5*(x - sin(x))
Then you would call your function like this:
fun = #(x) 8 - 4.5*(x - sin(x));
a = 2;
b = 3;
TolMax = .001;
root = BisectionRoot( fun,a,b,TolMax );
To debug (which you will need to do), use the debugger.
The command dbstop if error stops execution and opens the file at the point of the problem, letting you examine the variable values and function stack.
Clicking on the "-" marks in the editor creates a break point, forcing the function to pause execution at that point, again so that you can examine the contents. Note that you can step through the code line by line using the debug buttons at the top of the editor.
dbquit quits debug mode
dbclear all clears all break points

Calling function with changing input parameters in a loop Matlab

I have caught myself in a issue, I know its not that difficult but I couldnt figure out how to implement it. I have an m file that looks like
clear;
PVinv.m_SwF=20e3
for m=1:1:70;
PVinv.m_SwF=PVinv.m_SwF+1e3;
Lmin = PVinv.InductanceDimens();
Wa_Ac = PVinv.CoreSizeModel();
PVinv.CoreSelect(Wa_Ac);
[loss_ind_core,loss_ind_copper] = PVinv.InductorLossModel(PVinv.m_L_Selected);
Total_Inductor_Loss=loss_ind_core+loss_ind_copper
plot(PVinv.m_SwF,Total_Inductor_Loss,'--gs');
hold on
xlim([10e3 90e3])
set(gca,'XTickLabel',{'10';'20';'30';'40';'50';'60';'70';'80';'90'})
grid on
xlabel('Switching Frequency [kHz]');
ylabel('Power loss [W]');
end
And the function that is of interest is CoreSelect(Wa_Ac)
function obj = CoreSelect(obj, WaAc)
obj.m_Core_Available= obj.m_Core_List(i);
obj.m_L_Selected.m_Core = obj.m_Core_Available;
end
I want to change the value of i from obj.m_Core_List(1) to obj.m_Core_List(27) within that for loop of main m file. How can I get the value of the function coreselect when I call it in main m file
For eg for m=1 to 70 I want the function to take the value of i=1 then execute till plot command and then same with but i=2 and so on
Any suggestion would be really helpful
I'm not sure I understand your question perfectly, but I think you want to pass an index i to the CoreSelect function, and loop i from 1 to 27 outside of the function. Try this:
function obj = CoreSelect(obj, WaAc, i)
...
end
for i=1:27,
PVInv.CoreSelect(WaAc,i);
end

Passing variables into a function in Lua

I'm new to Lua, so (naturally) I got stuck at the first thing I tried to program. I'm working with an example script provided with the Corona Developer package. Here's a simplified version of the function (irrelevant material removed) I'm trying to call:
function new( imageSet, slideBackground, top, bottom )
function g:jumpToImage(num)
print(num)
local i = 0
print("jumpToImage")
print("#images", #images)
for i = 1, #images do
if i < num then
images[i].x = -screenW*.5;
elseif i > num then
images[i].x = screenW*1.5 + pad
else
images[i].x = screenW*.5 - pad
end
end
imgNum = num
initImage(imgNum)
end
end
If I try to call that function like this:
local test = slideView.new( myImages )
test.jumpToImage(2)
I get this error:
attempt to compare number with nil
at line 225. It would seem that "num" is not getting passed into the function. Why is this?
Where are you declaring g? You're adding a method to g, which doesn't exist (as a local). Then you're never returning g either. But most likely those were just copying errors or something. The real error is probably the notation that you're using to call test:jumpToImage.
You declare g:jumpToImage(num). That colon there means that the first argument should be treated as self. So really, your function is g.jumpToImage(self, num)
Later, you call it as test.jumpToImage(2). That makes the actual arguments of self be 2 and num be nil. What you want to do is test:jumpToImage(2). The colon there makes the expression expand to test.jumpToImage(test, 2)
Take a look at this page for an explanation of Lua's : syntax.