Matlab symbolic function conversion without dot for matrix operation - matlab

When converting symbolic expression to matlabFunction, expression like
x=sym('x')
f=- x^3/6 + x
g=matlabFunction(f)
-> #(x)x-x.^3.*(1.0./6.0)
which is not what I want because x is gonna be a matrix and my application requires actual matrix multiplication such as x^3 instead of the dot product form of x.^3
The only way to get it working is to use anonymous function, i.e.
g=#(x) - x^3/6 + x
->#(x)-x^3/6+x
However, the issue with anonymous function is that I cannot use substitution but to type the entire formulation, i.e.
g=#(x) f
-> #(x)f which shows that expression substitution does not work
In short, I will need to solve either one of the technical difficulties: (1) If I use matlabFunction, how do I remove all the dot after the conversion? or (2) If I use anonymous function, how do I bypass typing the symbolic expression if I have already defined 'f' for the expression?
I am totally lost here and I hope someone familiar with matlab can give me 2 cents.
Thank you!

You can convert the sym object to a string when calculating the anonymous function:
g=#(x)eval(char(f))
Alternatively, you can use the following code
h=eval(['#(x)' char(f)])
instead of matlabFunction

Related

Making symbolic functions without hard-coding in MATLAB

I want to make symbolic functions theta1(t), theta2(t), theta3(t),...,thetaN(t) where N is some parameter I can define in MATLAB. I know that I can use something like sym('theta',[1 N]) to get [theta1, theta2, theta3,..., thetaN]. However, how can I do the same thing with theta being a function of t? The way to hard-code it would be like syms theta1(t) theta2(t) theta3(t) ... thetaN(t), but I want to make this general.
I do not want to directly use the sym command here because "support of character vectors that are not valid variable names and do not define a number will be removed in a future release", meaning something like sym('theta1(t)') would not be valid in future releases.
Any suggestions?
Figured part of it out. I could do something like the following
for i = 1:N
syms(strcat('theta',num2str(i),'(t)'))
end
However, if I want to assign a variable that contains all the symbolic expressions I'm still stuck. If I try
for i = 1:N
my_array(i) = syms(strcat('theta',num2str(i),'(t)'))
end
I get Error using syms (line 133). Using input and output arguments simultaneously is not supported. It works if I use sym instead of syms, but this leads to the warning I mentioned in my original post.

using ode45 to solve y'=y adnd y'=t in Matlab

I first defined functions for dy/dt=y and dy/dt=t:
function dy=d(y):
dy=y
end
function ddy=dd(t):
ddy=t
end
And then I used ode45, respectively:
[t,y]=ode45('d',[1 10],1)
[t,y]=ode45('dd',[1 10],1)
which returns the following error: Error using d
Too many input arguments.
My question is:
Where did I go wrong?
How does Matlab know whether y or t is the independent variable? When I define the first function, it could be reasonably interpreted as dt/dy=y instead of dy/dt=y. Is there a built-in convention for defining functions?
First things first: the docs on ode45 are on the mathworks website, or you can get them from the console by entering help ode45.
The function you pass in needs to take two variables, y then t. As you noticed, with just one it would be impossible to distinguish a function of only y from a function of only t. The first argument has to be the independent, the second is the dependent.
Try defining your function as dy = d(t, y) and ddy = dd(t, y) with the same bodies.
one other note, while using a string representing the function name should work, you can use #d and #dd to reference the functions directly.

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.

Are there other ways to apply a function in Matlab?

If I want to use real() or abs() on some variable or vector like x, I have to write real(x) or abs(x). In Mathematica someone can use either Re[x] or Re#x. What is the equivalent of # in Matlab?
I mean using # is a shorthand notation for [] in Mathematica. Is there such a shorthand notation in Matlab?
As the other answers states; no, there is no short-hand notation similar to the ones you find in Mathematica. However, if you use the same combinations of functions often, you can create anonymous function handles that might improve the readability.
combinedfun = #(x) myfun1(myfun2(x))
or, if you want combinations of built-in functions:
absreal = #(x) abs(real(x))
In a real project I would not use anonymous functions like these, as there is not control of input arguments etc., but it might be handy if you want to do some calculations on the fly.
You can use arrayfun which needs a function to be applied on each element in x
Something like following :
arrayfun( #(y)abs(y), x )
However for simpler function simply use it on entire vector/variable/element :
abs(x)
There is no general shorthand notation for function input. But if your input arguments are strings then you can just use space to separate the function and its input arguments. So
strcat('a', 'b')
is same as
strcat a b
It cannot be used if the arguments are store in variables.

How to call a custom function with exponentiation for every element of a matrix in GNU Octave?

Trying to find a way to call the exponentiation function ( ^ ) used in a custom function for every item in a matrix in GNU Octave.
I am quite a beginner, and I suppose that this is very simple, but I can't get it to work.
The code looks like this:
function result = the_function(the_val)
result = (the_val - 5) ^ 2
endfunction
I have tried to call it like this:
>> A = [1,2,3];
>> the_function(A);
>> arrayfun(#the_function, A);
>> A .#the_function 2;
None of these have worked (the last one I believe is simply not correct syntax), throwing the error:
error: for A^b, A must be a square matrix
This, I guess, means it is trying to square the matrix, not the elements inside of it.
How should I do this?
Thanks very much!
It is correct to call the function as the_function(A), but you have to make sure the function can handle a vector input. As you say, (the_val - 5)^2 tries to square the matrix (and it thus gives an error if the_val is not square). To compute an element-wise power you use .^ instead of ^.
So: in the definition of your function, you need to change
result = (the_val-5)^2;
to
result = (the_val-5).^2;
As an additional note, since your code as it stands does work with scalar inputs, you could also use the arrayfun approach. The correct syntax would be (remove the #):
arrayfun(the_function, A)
However, using arrayfun is usually slower than defining your function such that it works directly with vector inputs (or "vectorizing" it). So, whenever possible, vectorize your function. That's what my .^suggestion above does.