Alphamax Macro expects END MATRIX command - macros

I try to call the alphamax macro in SPSS but when I try to run the command below, spss expects a 'end matrix' command, but this does not work:
alphamax 'variable names'.
Or this:
matrix.
alphamax 'variable names'.
end matrix.
I also tried DEFINE/ENDDEFINE but also not working..
Here is the Macro: http://afhayes.com/public/alphamax.sps

I ran this without a problem like this after running the alphamax file to get the macro defined. No quotes should be used in the invocation.
alphamax x y z.

Related

Running octave function prints lines of command name

Basically I'm running Octave 4.2.1 in Emacs 25.2.1 all within iTerm2 on macOS Sierra and every time I run a function (any function), a list of what look like command names is printed before the answer. I have tried to look up to see if this has been answered, but I'm having trouble describing the output list in a search. Here is the function command:
octave> f(3)
where f is declared as:
octave> function y = f(x)
>y = x + 10;
>endfunction
and the output is:
yes_or_no
ylabel
ylim
yulewalker
x
xlabel
xlim
xor
end
end_try_catch
end_unwind_protect
endfor
endfunction
endgrent
endif
endparfor
endpwent
endswitch
endwhile
endfunction
endfunction
ans = 13
I have tried placing semicolons at the end of every line (because I was unsure if this is function output that could be silenced with a semicolon), but that didn't pan out.
Edit: I have also tried turning off diary because I'm not sure that diary is. Needless to say, did not work out.
Let me know what I can do to stop this output. And if this is a duplicate due to me not having the vocabulary to search for my solution, obviously just mark it and I'll follow the link.
Thanks!

Using fzero to solve eqn in MatLab

I hope this is the right area. I'm trying to get this code to work in MatLab.
function y=test(x)
y=-x+(B/(B-1))*(r-a)*p+(B/(B-1))*(r-a)*(b((1-(b/x)^(B-1))/r- a)+p* ((b/x)^B))/(1-(b/x)^B);
end
I then jump to the command value and type this:
B=3.0515;
b=1.18632*10^5;
a=.017;
r=.054;
p=5931617;
I then try to find the zeros of the first equation by typing this and I get errors:
solution=fzero(#test,5000000)
I'm getting the following error:
Error: File: test.m Line: 5 Column: 1 This statement is not
inside any function. (It follows the END that terminates the
definition of the function "test".)
New error
Error using fzero (line 289)
FZERO cannot continue because user supplied function_handle ==> #(x)
(test(x,B,b,a,r,p))
failed with the error below.
Subscript indices must either be real positive integers or logicals.
I would guess that this is a problem of scoping, you are defining variables (B, b, etc...) in the command line but trying to use them inside your test function where they are out of scope. You should alter your test function to take these in as parameters and then use an anonymous function so that your call to test in fsolve still only takes a single parameter:
function y=test(x, B, b, r, a, p)
y=-x+(B/(B-1))*(r-a)*p+(B/(B-1))*(r-a)*(b((1-(b/x)^(B-1))/r- a)+p* ((b/x)^B))/(1-(b/x)^B);
end
and
B=3.0515;
b=1.18632*10^5;
a=.017;
r=.054;
p=5931617;
solution=fzero(#(x)(test(x,B,b,a,r,p)),5000000)
As an aside, unless you really do mean matrix multiplication, I would suggest that you replace all your *s and /s in test with the element-wise operators .* and ./. If you are dealing with scalars, it doesn't matter now, but it makes a big difference if you later want to scale your project and need a vectorized solution.
Regarding the errors you have added to your question:
You can't put code after the end in your function file. (With the exception of local functions). Your objective function should be an .m-file containing the code for one single function.
This is because in your test function you have ...b((1-(b/x)^(B-1))... which in MATLAB means you are trying to index the variable b in which case the value of (1-(b/x)^(B-1) has to be a positive integer. I'm guess you are missing a *
Your
function y=test(x)
y=-x+(B/(B-1))*(r-a)*p+(B/(B-1))*(r-a)*(b((1-(b/x)^(B-1))/r- a)+p* ((b/x)^B))/(1-(b/x)^B);
end
cannot access variables in your workspace. You need to pass the values in somehow. You could do something like:
function y=test(x,B,b,a,r,p)
y=-x+(B/(B-1))*(r-a)*p+(B/(B-1))*(r-a)*(b((1-(b/x)^(B-1))/r- a)+p* ((b/x)^B))/(1-(b/x)^B);
end
and then you can create an implicit wrapper function:
B=3.0515;
b=1.18632*10^5;
a=.017;
r=.054;
p=5931617;
solution = fzero(#(x) test(x,B,b,a,r,p),5000000)
I haven't tested whether fzero returns sensible results, but this code shouldn't give an error.

MATLAB error message "This statement is not inside any function."

I am trying to define a simple function and then call it:
function p=MyExp(N);
p=[ 1 ]; % 0th order polynomial.
for k=1:N
pk=1/(factorial(k));
p=[pk,1];
end
end
poly3=MyExp(3);
disp (poly3)
MATLAB is returning a message:
Error: File: matlab_labIII_3_I.m Line: 10 Column: 1
This statement is not inside any function.
(It follows the END that terminates the definition of the function
"MyExp".)
This script works well on OCTAVE!
Thanks
If you use functions in a Matlab script, you are expected to have all code inside of function(s), of which there can be more than one. Similar products (Octave and Scilab) do not have this restriction.
There's an easy way out with minimal change of code: wrap the non-function code into a function, and invoke that. The main function should appear first in the script.
function MyProgram()
poly3=MyExp(3);
disp (poly3)
end
function p=MyExp(N);
p=[ 1 ]; % 0th order polynomial.
for k=1:N
pk=1/(factorial(k));
p=[pk,1];
end
end
Also, when you use functions, Matlab expects the name of your file to match the name of the function to be called. So, the file should be named MyProgram.m (or whatever your main function is named).

evalin with variable - MatLab

I'm trying to do something like evalin('base','mat(x)', 4), where mat is a matrix in my main script, and x is a variable in the function I'm running this from. How can I use the x variable in there?
Try this:
evalin('base',['mat(' num2str(x) ')'])
This substitutes x for the value it has in your function, and uses that value to build the string that is passed to evalin.

MATLAB symbolic variables couldn't be used in nested function

I have a MATLAB function to solve a Inertia Tensor , and I have a nested function in my program . All the variables in it are symbolics but it told me
“Error using assignin: Attempt to add ”x“ to a static workspace”
and I don't understand why this happens . Here is my test.m code:
function test
syms x y z
f=x
f1=f+1
f2=f1^2
function r=test2
r=f2^3;
end
f3=test2
end
After searching this web-forum I have found some answers . But at the same time I just don't understand it
Andrew Janke explianed it like this : While syms A may look like a static variable declaration, it isn't. It's just a regular function call. It's using Matlab's "command" invocation style to look like syntax, but it's really equivalent to syms('a', 'b', 'c').
on this page : Matlab: "Error using assignin: Attempt to add "c" to a static workspace"
what does static variable mean ?
I also search the HELP doc and it said :In functions and scripts, do not use syms to create symbolic variables with the same names as MATLAB® functions. For these names MATLAB does not create symbolic variables, but keeps the names assigned to the functions.
I only know syms x to create a symbolic variable in the workspace but why does the documentation say MATLAB does not create ?
'Static' means fixed, 'workspace' is what Matlab calls the places where all of its variables are stored. For non-nested functions the workspace starts off as empty when Matlab is at the beginning of the function; as Matlab continues through function's lines of code it continuously add more variables to the workspace.
For functions with a nested function, Matlab first parses the function to see what variable will be created (it specifically looks for x = type lines), then it creates all of these variables (with value as 'unassigned'), and then only does it start to run through the code; but while running through the code, it can never create a new variable.
This is why the code
function TestNestedFunction
syms x;
function Nested()
end
end
generates an error, there is no x = to tell it to pre-create the unassigned variable x at the start of the code. It fails at syms x;, as that line tries to create a new variable x, which fails as it may not.
This is also why the following code runs
function TestNestedFunction
syms x;
x = x;
function Nested()
end
end
it sees the x = and then pre-creates x. (This is why your example of adding [x, y, z] = deal([]); also works).
You can test this with a break point at the beginning of simple non-nested function and a simple nested function. Just run it step by step.
This code works:
function test
x=sym('x')
y=sym('y')
z=sym('z')
f=x
f1=f+1
f2=f1^2
function r=test2
r=f2^3;
end
f3=test2
end
I think the pages you found are quite clear.
You need to declare the variables one by one and use:
x = sym('x')
Otherwise syms will try to assign the values into a workspace where this is not allowed.