Maple - subscripted variable T_0 isn't evaluated - maple

I have a subscripted variable T_0 in Maple with a value assigned to it T_0 := 1.
When I call the variable by entering T_0 in math mode, T_0 is displayed instead of 1. This doesn't happed for non-subscripted variables.
Why are subscripted variables not evaluated? I noticed this only happens for the variable name T and not for a; a_0 works.

It turned out that whitespaces after the index in the subscript are interpreted by Maple. Removing these whitespaces solved the problem.

Related

How does the MATLAB symvar function work?

I read the documentation and help about the symvar function but I'm still confused about how it works, especially for the following example:
syms x y a
symvar(x + y, 1)
When I run this M-file, I get the answer 'x'. Why am I getting this answer? Why not 'y'? And what is the role of the number 1 written as the second argument?
I'm guessing you typed help symvar into the MATLAB Command Window, which is normally the best first step, but in this case you may have come across a small pitfall. This is because symvar is an overloaded function: there is more than one copy, and the copy that gets called depends on the data type/class of the variables/objects that are passed to it. You can see all the versions by using the which function with the -all option. The output I get on R2018a is:
>> which symvar -all
C:\Program Files\MATLAB\R2018a\toolbox\matlab\funfun\symvar.m
C:\Program Files\MATLAB\R2018a\toolbox\curvefit\curvefit\#fittype\symvar.m % fittype method
C:\Program Files\MATLAB\R2018a\toolbox\symbolic\symbolic\#sym\symvar.m % sym method
C:\Program Files\MATLAB\R2018a\toolbox\matlab\funfun\#inline\symvar.m % inline method
Notice how there is a default version in ...\matlab\funfun, and then three more versions for fittype, sym, and inline objects. When you type help symvar you get the help for the first one, which isn't very helpful because you want the help for the overloaded sym method. To get this, you should type help sym/symvar, and you'll see something like this:
symvar Finds the symbolic variables in a symbolic expression or matrix.
symvar(S), where S is a scalar or matrix sym, returns a vector sym
containing all of the symbolic variables appearing in S. The
variables are returned in lexicographical order. If no symbolic variables
are found, symvar returns the empty vector.
The constants pi, i and j are not considered variables.
symvar(S,N) returns the N symbolic variables closest to 'x' or 'X'.
If N exceeds the number of variables appearing in S, or equals inf,
then all variables appearing in S are returned.
Upper-case variables are returned ahead of lower-case variables.
If S is a symbolic function the inputs to S are listed in front of the
other free variables.
Examples:
syms alpha a b x1 y
symvar(alpha+a+b) returns
[a, alpha, b]
symvar(cos(alpha)*b*x1 + 14*y,2) returns
[x1, y]
symvar(y*(4+3*i) + 6*j) returns
y
You can also use the online documentation for the newest MATLAB version: symvar
These make it clear what symvar does for symbolic variables. It will return a vector of all symbolic variables it finds in the expression you pass it as the first argument. If you specify a second argument as a number, it will only return up to that many symbolic variables, choosing first the ones that are alphabetically closest to 'x'.

Is this a bug in Matlab function call?

The code I am using here is for illustration of the possible bug. In the code I defined three functions as below and tried to visualize them.
The first:
$$y_1(x)=5\sin(x)$$
The second:
$$y_2(x)=12-8\cos(x)$$
The 3rd is a piecewise combination of the above two:
when x<0:
$$y_3(x)=y_1(x)$$
when x>=0:
$$y_3(x)=(y_1(x)+y_2(x))/2$$
When I run the following code in Matlab, which is saved as m2mPlot.m :
function m2mPlot
clear all
close all
clc
global a b c;
a=12;
b=8;
c=5;
t=-pi:.1:pi;
plot(t,y1(t),'b')
hold on
plot(t,y2(t),'m')
plot(t,y3(t),'r')
legend('y1','y2','y3')
function y=y1(t)
% The first function for testing
global c;
y=c*sin(t);
function y=y2(t)
% The 2nd function for testing
global a b;
y=a-b*cos(t);
function y=y3(t)
% The 3rd function for testing
if t<0 % It seems this logic value is always FALSE, why?
y=y1(t);
else
y=(y2(t)+y1(t))/2;
end
I got:
which indicates that in the third sub-function the logic expression: t<0 is always FALSE no matter what value t actually is.
Is this a bug in Matlab? How to avoid such a problem?
This is not a bug in MATLAB, you're just using it incorrectly.
You are calling y3(t) where t is a vector, i.e. t=-pi:.1:pi;. But the code for y3 uses t in a conditional, i.e. if t<0. Since the result of t<0 is a vector, and the if statement expects a scalar, it will not work as you expect. As Troy Haskin points out in the comments, from the MATLAB docs:
if expression, statements, end evaluates an expression, and executes a group of statements when the expression is true. An expression is true when its result is nonempty and contains only nonzero elements (logical or real numeric).
Otherwise, the expression is false.
Your t<0 vector contains many false values and so the if evaluates it as false. I would advise you to simply only ever give a MATLAB if a scalar.
If you want to create your y3 function in a vectorized manner try this instead:
function y=y3(t)
y=y1(t).*(t<0) + y2(t).*(t>=0);
end

Vectorizing a symbol with more than one value in a for loop in MATLAB

I would like to export the answers of an equation with the order of 2 into a vector. The input is R01and the variable is n.
The problem is where I want to "double" the symbol for each step of i, I get the following error:
In an assignment A(I)=B, the number of elements in B and I must be the
same.
There will be no error if I do not use a for loop. What is my mistake and how can I modify it that I could get the data as a matrix or vector.
R01=[0.07941 0.07942 0.07952 0.07946 0.07951 0.07947]
syms n
for i=1:length(R01)
eq3=((1+n)^2)*R01(i)-(n-1)^2
sol1=solve([eq3]);
nsol(i)=double(sol1);
end
The efficient way to solve the problem (by Daniel):
syms n
for i=1:length(R01)
eq3=((1+n)^2)*R01(i)-(n-1)^2
sol1=solve([eq3]);
nsol(i,:)=double(sol1);
end

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.

Tilde character in the brackets

In MATLAB, what does the following code do:
[m, ~]=func_returning_matrix()
What does the tilde operator, ~, do?
In Matlab it means don't assign the corresponding output argument(s) from the function on the rhs of the assignment. So, if func_returning_matrix returns 2 arguments, the expression will assign the first to the variable m and forget the second. If func_returning_matrix returns 3 (or more) arguments, then the expression will drop the second and all later outputs from the function.