Trouble with fortran( ) to generate FORTRAN code from MATLAB symbolic expression - matlab

I did a number of symbolic manipulations and obtain an expression for my variable z. I used the following code to generate the FORTRAN code for z:
fortran(z,'file','FTRN_2Mkt_dfpa1');
The program stops after about 10 min, and I get these error messages:
??? Error using ==> mupadmex
Error in MuPAD command: Recursive definition [See ?MAXDEPTH]; during evaluation of 'generate::CFformatting'
Error in ==> sym.sym>sym.generateCode at 2169 tk = mupadmex(['generate::' lang], expr, 0);
Error in ==> sym.fortran at 43 generateCode(sym(t),'fortran',opts);
I think the problem is that the z expression is too long. The MuPAD software is treating this long expression as an infinite recursive operation. I am guessing that the MAXDEPTH in the fortran() source file is set at a level that is smaller than needed to convert the z expression to FORTRAN. If my guess is correct, is there a way to change MAXDEPTH in the fortran() source code?
If my guess is wrong, what can I do to generate the FORTRAN code for the z expression?
I really need the FORTRAN code for the symbolic expression of z. If you can help me, it would be wonderful. Thanks a million in advance!
Best, Limin

Related

Issues with MATLAB butter

I am using MATLAB R2015 and cannot implement 'butter' without getting an error related to too many output arguments from the polyfit part of the script. I have used the same implementation for years with other versions of MATLAB.
Example:
[b, a] = butter(2,[.15,.3]);
Error using poly
Too many output arguments.
Error in zp2ss (line 127)
den = real(poly(p(i:i+1)));
Error in butter (line 97)
[a,b,c,d] = zp2ss(z,p,k);
I get the same error implementing the examples in the help documentation.
Just a guess but is there any chance you've defined your own poly function? which('poly') should point to some Matlab directory unless you've defined it elsewhere, potentially as a variable?. For me on a newer version that path is something like $MATLAB/toolbox/matlab/polyfun/poly.m You can also edit the poly function edit poly and verify that there is one output argument for the function.
Also, I'd advise against ever writing decimal numbers without a leading zero. It took me way to long to figure out what [.15,.3] was. Instead write [0.15,0.3] or even just [0.15 0.3] Edit: I just realized that is an example in Matlab ... my point stands but the toolbox author should know better ...

Symbolic Math Toolbox hitting divide by zero error when it used to evaluate to NaN

I've just updated to Matlab 2014a finally. I have loads of scripts that use the Symbolic Math Toolbox that used to work fine, but now hit the following error:
Error using mupadmex
Error in MuPAD command: Division by zero. [_power]
Evaluating: symobj::trysubs
I can't post my actual code here, but here is a simplified example:
syms f x y
f = x/y
results = double(subs(f, {'x','y'}, {1:10,-4:5}))
In my actual script I'm passing two 23x23 grids of values to a complicated function and I don't know in advance which of these values will result in the divide by zero. Everything I can find on Google just tells me not to attempt an evaluation that will result in the divide by zero. Not helpful! I used to get 'inf' (or 'NaN' - I can't specifically remember) for those it could not evaluate that I could easily filter for when I do the next steps on this data.
Does anyone know how to force Matlab 2014a back to that behaviour rather than throwing the error? Or am I doomed to running an older version of Matlab forever or going through the significant pain of changing my approach to this to avoid the divide by zero?
You could define a division which has the behaviour you want, this division function returns inf for division by zero:
mydiv=#(x,y)x/(dirac(y)+y)+dirac(y)
f = mydiv(x,y)
results = double(subs(f, {'x','y'}, {1:10,-4:5}))

Double inverting symbolic

When I ran a simple exercise of integration of defined functions as below,
clear all;
syms z tau;
deltav=tau^(1/(3*z))-tau^(1/(4*z));
deltax=1/(0.5+12*z)*(tau^(1/(3*z))-tau^(1/(4*z)));
a=1;
b=9;
tau=0.5;
mu_vx=int(deltav*deltax,a,b);
mu_x2=int(deltax^2,a,b);
ratio=double(mu_vx/mu_x2);
I got error message from MATLAB by saying "??? Error using ==> mupadmex
Error in MuPAD command: DOUBLE cannot convert the input expression into
a double array.If the input expression contains a symbolic variable, use the VPA
function instead."
So I plug in the expression of "deltav" and "deltax" in to integration, and run
clear all;
syms z tau;
deltav=tau^(1/(3*z))-tau^(1/(4*z));
deltax=1/(0.5+12*z)*(tau^(1/(3*z))-tau^(1/(4*z)));
a=1;
b=9;
tau=0.5;
mu_vx=int((tau^(1/(3*z))-tau^(1/(4*z)))*1/(0.5+12*z)*(tau^(1/(3*z))- tau^(1/(4*z))),a,b);
mu_x2=int((1/(0.5+12*z)*(tau^(1/(3*z))-tau^(1/(4*z))))^2,a,b);
ratio=double(mu_vx/mu_x2)
It works this time. I wonder how I should make the first way work without plugging the long expression. Thank you.
Sometimes a solve is necessary to find a explicit solution
mu_vx=int(solve(deltav*deltax),a,b);
mu_x2=int(solve(deltax^2),a,b);
ratio=double(mu_vx/mu_x2);

Matlab table function

I have tried running the example as shown in the documentation of MuPAD table:
T := table(a = 13, c = 47)
Doing so gives me the following error:
Undefined function 'T' for input arguments of type 'char'.
I have no idea what is going on. Does anybody know why it isn't working and how I can create a table using this function?
If you look at the top of the page, you are reading about functions contained in the "Symbolic Math Toolbox." I believe you need to pay for that license to use functions from the toolbox.
From Matlab's help for that error you either:
Made a typographical error ...
Changed directories so that a function is no longer on the search path...
Used the wrong case for a function or variable name ...
Are trying to use a function for which you are not licensed.
I got the same error as you, and I also do not have that Toolbox, so perhaps its the last reason.
Is it time to switch to Python?
This happens when you try to call a muPAD function from the MATLAB command line. Whenever you see :=, that's a clue that you're dealing with muPAD. You cannot use muPAD syntax directly in MATLAB (feval or evalin and symengine can be used in some cases to call muPAD functions and return a symbolic expression).
To use table in muPAD:
Call mupad at the command line to open up a muPAD notebook, then call your sample line. You do not need the latest MATLAB version, although I'm not sure exactly when it was brought in (works fine for me on 2011b with Symbolic toolbox).
To use table in MATLAB:
The muPAD table function should not be confused with the MATLAB table function/datatype, which is relatively new. The equivalent in MATLAB of that muPAD code would be something along the lines of (untested):
T = table([13;47],'RowNames',{'a';'c'});

??? Input argument "x" is undefined

I enter:
EDU>> %using the temporary variable levels
EDU>> levels=range/quantise_range;
levels=round(levels);
quantisation_bits=log2(levels)
NB. There is no x variable anywhere
My error is:
??? Input argument "x" is undefined.
Error in ==> range at 18
y = max(x) - min(x);
EDU>> %combining above process into one statement
quantisation_bits=log2(round(range/quantise_range));
??? Input argument "x" is undefined.
Error in ==> range at 18
y = max(x) - min(x);
Would someone care to explain thie issue? I am beginner into programming and I really don't understand how to read the error hint.
Thanks.
Additionally, in what may be due to whatever the same principle misunderstanding on my part is, I am finding trouble here with this code, with error also included:
%Trying to create my own function, I’m pressing shift+enter at the end of lines for neatness:
EDU>> function what_am_i()
disp 'I am a function'
??? function what_am_i()
|
Error: Function definitions are not
permitted in this context.
EDU>>
function what_am_i()
disp' I am a function'
??? function what_am_i()
|
Error: Function definitions are not
permitted in this context.
EDU>> end
??? end
|
Error: Illegal use of reserved keyword
"end".
You may be confusing the "Command Window" with the "Editor".
In the "Command Window" you can enter some lines of code, but can't create functions. It acts more like a calculator.
If you create and save files, then you open them in the "Editor" and that's when Matlab begins behaving more like a programming language. It saves .m files that can have functions or algorithms written in them.
Matlab has lots of help available. I recommend visiting their website and searching around a bit. (or just google "intro to matlab")
In addition to #user1860611's answer regarding function definitions in the command window, the other problem you have has to do with range, which is a built-in function. It appears you are trying to use range it as a variable name, but didn't actually initialize it to a value, so it is still a function.
In the line here:
levels=range/quantise_range;
you are essentially calling the range function, but without passing it an argument.
Error in ==> range at 18
y = max(x) - min(x);
The error message is telling you that a function called range has generated an error. It doesn't matter that you don't have a variable named x, because the function has one internally.