Let us suppose that we run the following set of commands in Octave:
pkg load symbolic %loads the symbolic math package
syms x y %declare x and y symbols
f = x^2 - 2*x + 3;
V = [-5:0.25:5]';
V_x = subs(f, x, V)
At this point V_x is a symbolic expression in Octave. Now, if this were to be MATLAB, one would run eval(V_x) and everything would be converted to numbers. However, eval does not seem to run in Octave as in MATLAB.
What should be done to convert the symbolic array into numbers?
double has been overloaded for symbolic variables so you can use double to explicitly convert the symbolic result to it's numeric representation
V_x_num = double(V_x);
This works in MATLAB as well as Octave.
and how about getting variable precision (number of digits) in the evaluated symbolic output, ie, staying in the vpa symbolic space but solving all the sym internal functions to digits
eval still outputs in the default octave output_precision and format long limitations, so that is of no use.
this is one way (sym variable y holds the value):
sympref digits 1000
syms x
eqn = x==y
vpasolve(eqn)
Related
I've been trying to display in my console an exponential equation like the following one:
y(t) = a*e^t + b*e^t + c*e^t
I would write it as a string, however the coefficients a,b and c, are numbers in a vector V = [a b c]. So I was trying to concatenate the numbers with strings "e^t", but I failed to do it. I know scilab displays polynomial equations, but I don't know it is possible to display exponential one. Anyone can help?
Usually this kind of thing is done with mprintf command, which places given numerical arguments into a string with formatting instructions.
V = [3 5 -7]
mprintf("y(t) = %f*e^t + %f*e^t + %f*e^t", V)
The output is
y(t) = 3.000000*e^t + 5.000000*e^t + -7.000000*e^t
which isn't ideal, and can be improved in some ways by tweaking the formatters, but is readable regardless.
Notice we don't have to list every entry V(1), V(2), ... individually; the vector V gets "unpacked" automatically.
If you wanted to have 2D output like what we get for polynomials,
then no, this kind of thing is what Scilab does for polynomials and rational functions only, not for general expressions.
There is also prettyprint but its output is LaTeX syntax, like $1+s+s^{2}-s^{123}$. It works for a few things: polynomials, rational functions, matrices... but again, Scilab is not meant for symbolic manipulations, and does not really support symbolic expressions.
I wrote this code in octave:
syms z;
f=z-2;
fsolve("f",0.)
Then this gives the error
#f: no function and no method found.
Also using fsolve(#f,0) gives the same error
When I write code as:
syms z;
f=z-2;
fsolve(f,0.)
Then this gives the error
ind2sub: subscript indices must be either positive integers less than 2^31 or logicals.
Please explain to me how to actually use fsolve.
% syms z; % Not needed, actually slows down the code
f=#(z)(z-2);
fsolve(f,0.)
You're missing the # symbol, which is a function handle. This tells Octave that f is not a variable, but actually is a(n anonymous) function, in this case of z, which is the first argument.
You'll probably want to have z to be a regular variable, because making it symbolic turns MATLAB from a speeding race car to a drudging farm vehicle. Unless there's a specific reason to have z symbolic (I cant think of any in case of usage with fsolve)' it's best to avoid symbolic maths.
I want to define the mathematical function sum(x^z)+z where x ranges from 1:10 and z is a variable
One way I tried is
syms z
f=#(z)(z)
for i=1:10
f=f+(i.^z)
end
But this leads to a error: binary operator '+' not implemented for 'function handle' by 'matrix' operations
I also tried to define a function initially of two variables x and z as
syms x
syms z
f(x,z)=z+(x.^z)
sum(f([1:10,z]))
It also gives error
Please tell me the correct method to do this.
In octave what is the difference between log(a) and log a?
>>a
a =
1 2
>>log(a)
ans =
0.00000 0.69315
>>log a
ans = 4.5747
In the second example, Octave is interpreting 'a' as a char, converting 'a' to its ASCII representation (97) and then getting the natural logarithm.
log(97) = 4.5747
In general you have two ways to call functions: as a function or as a command. E.g.
save('test.txt')
save test.txt
When a function is used as a command, it assumes the input is a string.
Anyway newer version of Matlab and Octave have an error check for character input (there is little reason to compute the logarithm of the ASCII equivalent of a character).
A software library originally written for MATLAB, comprising of MATLAB and C source files, is being ported to Octave. The C code uses the MATLAB MEX file interface. The library works without error on MATLAB, but not on Octave. The C source is closed and I don't have access to it, but someone kindly compiled it for me.
The following Octave code
Y=ones(size(X)) + X;
fails with the error
Subscript indices must be either positive integers or logicals.
X is a matrix returned by the MEX module.
I've already verified that ones and size are referring to the builtin functions and not overwritten by some local variables.
How can I fix this?
EDIT
Breaking down into steps:
S=size(X);
O=ones(S);
X+O;
gives the above error on the last line, the addition. The whos command outputs this:
octave:13> whos O X
Variables in the current scope:
Attr Name Size Bytes Class
==== ==== ==== ===== =====
O 512x512 2097152 double
X 512x512 2097152 double
Total is 524288 elements using 4194304 bytes
The error you report has no reason to be. I can't figure out what it may be doing. If I generate the X matrix you report, it all works fine:
X = rand (512, 512);
S = size (X);
O = ones (S);
X+O;
I don't know how you confirmed that you were using the builtin functions, so can you check that this works:
X = rand (512, 512);
S = builtin ("size", X);
O = builtin ("ones", S);
X+O;
Or could it be that the mex file someone compile for you somehow overloads the plus operator for double? Since you don't have the source for it, I'd suggest you do the following. After calling the mex function, save X, exit, and load it in a new Octave session. Check that the error disappeared, and if not, share the file with us so that we can at least try to reproduce it.
X = your_closed_source_mex (...);
save -binary data.dat X
exit();
Then start a new Octave session:
load -binary data.dat
whos X # confirm that X is loaded
X = rand (512, 512);
S = size (X);
O = ones (S);
X+O;