My function in Octave doesn't work - function

I'm working with Octave4.2.1 and I have written this function (in the file OctaveFunction.m):
function y = squareNumber(x)
y = x^2;
endfunction
but if I call the function, I get this error:
error: 'squareNumber' undefined near line 1 column 1
and if I try to call the function in this way:
OctaveFunction squareNumber(4)
I get another error:
warning: function name 'squareNumber' does not agree with function filename 'C:\Users\HOME\Desktop\OctaveFunction.m'
error: for x^A, A must be a square matrix. Use .^ for elementwise power.
error: called from
OctaveFunction at line 2 column 7
Where did I go wrong? Thank you!

I think the main problem is that your file name does not match the function name. If you were to match these, this should resolve your first error.
Regarding the elementwise power error: If given the proper input (4) this should not lead to an error, as 4 is obviously a square matrix.
Hence it seems that some undesired input is fed to your function, but again this problem is likely to disappear if you rename the file to match the functionname, and call the function as usual. (so without OctaveFunction).

You Must rename your file such as your function name
for example: your file name is (main.m) and your function name is (main)

Related

MATLAB | Invalid syntax for calling function 'cond' on the path. Use a valid syntax or explicitly initialize 'cond' to make it a variable

I want to extract data from a table called cond. As you can see from line 75 in the screenshot shown below, data Diameter corresponding to Drake can be successfully extracted using cond('Drake',:).Diameter.
screenshot
However, when I was trying to write this into a function called findCF(), things went wrong at line 78 with an error message
Invalid syntax for calling function 'cond' on the path. Use a valid
syntax or explicitly initialize 'cond' to make it a variable.
Can anybody tell me how to modify my code?
cond() is the name of a built-in function. Matlab tolerates variables whose names collide with functions, but it can result in weird things like this. In the line that produces the error, Matlab thinks you are trying to call the function cond(), not access the variable cond.
Rename the variable to something else.

Global variable and error when the function is running in Octave

Program (programmatically implement) the problem of solving the rigid equation eq = -lambda * (y-sin (x)) using the ode45 solver in the Octave environment.
There is code
lambda=10#Global variable for function
function slv=fi(x)#Equation solution
C=1+(lambda/(1+pow2(lambda)));
slv=C*exp(-lambda*x)-(lambda/(1+pow2(lambda)))*(-lambda*sin(x)+cos(x));
end
#The initial function of the equation
function y=g(t,x)
y=-lambda*(x-sin(t));–1 ERROR
end
%Determination of parameters for controlling the course of the equation
optio=odeset("RelTol",1e-5,"AbsTol",1e-5,'InitialStep',0.025,'MaxStep',0.1);
[XX,YY]=ode45(#g,[0 0.25],2,optio); - 2 ERROR
#Exact solution
x1=0:0.05:0.25;
y1=fi(x1);
%Function solution graph with ode45 and exact solution
plot(x1,y1,'-g:exact solution;',XX,YY,'*b;ode45;');
grid on;
But there are some errors:
lambda = 10
error: 'lambda' undefined near line 11, column 11
error: called from
g at line 11 column 4
runge_kutta_45_dorpri at line 103 column 12
integrate_adaptive at line 135 column 39
ode45 at line 241 column 12
LabWork6 at line 18 column 8
Due to the lambda variable in the function, the error moved to another part of the program.
What did I do wrong when declaring a lambda?
Variables at 'global' scope in a script are not visible from within functions, even if defined on the command-line instead of their own file, as it the proper way.
This makes sense if you think about it, because command-line functions are just convenience syntax, and a file-defined function would have no idea that some other script defines a variable somewhere, unless you use the global keyword to tell it about that variable.
So either declare lambda as global both outside and inside your function, or simply wrap your entire script inside a function, which then allows 'nested' functions to see/inherit parent-function variables.

Can I call a function to solve for different variables?

I have a function where I want to solve for many variables separately, do I have to write down the function every time in terms of the other variable?
x,xG,xR
y = e.^tan(x.^2)+cos.^2(x);
yG = e.^tan(xG.^2)+cos.^2(xG);
First you cannot write an expression like cos.^2(x). If x is a single variable (ie x=pi) you could write either cos(x)^2 or cos(x^2). If x is a vector (a column vector might be x=[3;4;pi] and a row vector might be x=[3,4,pi], then you might write cos(x).^2 or cos(x.^2). The role of the period (.) in octave is explained here: https://octave.org/doc/v4.0.3/Arithmetic-Ops.html
Another issue has to do with understanding the difference between an expression: x=e^tanh(y); and a function. The later is a separate chunk of code that can be invoked from anywhere in your program.
Consider this simple example
1;
function y=myfunc(x)
y=exp(tanh(x));
endfunction
## main program
xxx=pi/3;
yyy=myfunc(xxx);
printf('%7.3f %7.3f\n',xxx,yyy)
y=exp(tanh(pi/3))
comments: The '1' in the first line tells Octave that there is more to the script than just the following function: the main program has to be interpreted as well. The function line specifies that inside the function, the input will be called x and the output y, so when my function is called from main, the input is xxx(=pi/2) and the output is yyy. The last line in this tiny script is an expression that does the same thing as the function. Note that since I didn't include a semi-colon at the end of that line the result is printed out
I suggest you play with this for a while, then if you have more questions, ask them in a new question.

Index out of Bounds error in a small Octave program

This is the code, it just calculates two outputs based on two inputs x and y.
function [nextX, nextY]=newton(x,y)
nextX= x-((6*(x^2)*(y^2)*(x+y)-9*(y^2)((y^3)+14)-2*(x^2)*(5*(x^2)-69))/(x*y*(18*x*y+20)));
nextY= y-((10*(x^4)+18*x^2*(y^3)-414*(x^2)+30*x*(y^2)-420*x)/(x*y*(18*x*y+20)));
end
[x,y]=newton(1,1)
I get
error: newton: A(I): index out of bounds; value 15 out of bound 1
error: called from:
error: newton at line 2, column 6
whenever I try to run it, I'm a novice at octave and I really can't see what I'm doing wrong here.
You have a very simple error (probably a typo) in your code here:
nextX= x-((6*(x^2)*(y^2)*(x+y)-9*(y^2)((y^3)+14)-2*(x^2)*(5*(x^2)-69))/(x*y*(18*x*y+20)));
^
There is no operator between two parentheses, so Octave assumes that you're trying to get an element of a vector/matrix by it's index, thus throwing index out of bounds exception.
Probably you wanted to multiply two values instead:
nextX= x-((6*(x^2)*(y^2)*(x+y)-9*(y^2)*((y^3)+14)-2*(x^2)*(5*(x^2)-69))/(x*y*(18*x*y+20)));

Functions and function name errors

I'm trying to write a simple function, e.g:
function [x y] = functionname (a, b, c, d, e)
so I create an m-file called function-name.m, with all the variables specified. However, the m-file from which I'm calling functionname does not like the name, and insists on calling it the actual name of the m-file (i.e, I get the error not enough input arguments).
How do I get around this?
You can't get around it. In MATLAB you have to name the file with the name of the function. Any functions in that file that have a different name are private (not visible to any code other than the code inside that m-file).
The MATLAB documentation states that the name of the file and the function must be identical:
Save the function code in a text file with a .m extension. The name of the file should match the name of the first function in the file. Valid function names begin with an alphabetic character, and can contain letters, numbers, or underscores.
However, what actually matters in practice is the name of the file, not the name of the main function declared in that file. For instance, if your file is named func.m, but the function inside is defined function functionname(...), you'll need to invoke it as func(), not functionname(). Try it!
In your case it's even worse, because the file name is "function-name.m", so it contains a hyphen. Hyphens are not allowed in function names (MATLAB interpret them as minus signs), so you are basically stuck with a function that you cannot invoke.
The bottom line is that if you don't want trouble, do as MATLAB wants and keep the file and function named identically.