I want to create a matrix by "zeros([numbers,3])"
syms x;
numbers=symsum(x,x,1,5);
zeros([numbers,3])
Here is the error: The size must be a number.
How to create a matrix like this "zeros([numbers,3])"
This is because numbers is of sym class. Convert it to double first. i.e. use XX=zeros(double(numbers),3); instead.
But still there is no need to use Symbolic Math Toolbox here. What you're doing can be done more simply as:
numbers = sum(1:5);
XX = zeros(numbers,3)
Related
I need to calculate the mean and the variance of training set for anomaly detection but keep getting dimension mismatch error.
mean_train = 1/(num_train )* sum(Xtrain);
var_train = 1/(num_train )* sum(Xtrain - mean_train).^2;
First of all show all data, because we can't reproduce your mistake.
I suggest, you take this error because of this:
mean_train or num_train is a vector and then we can't simply multiply it or minus it with Xtrain.
But the answer for you:
just use build-in Matlab functions mean and var. This let you use it in this way:
mean(Xtrain)
avoiding using secondary variables like num_train.
I checked your code and found mistake:
Xtrain is an 100x2 array. And using sum on it returns 1x2 array! You can read about this in help:
If A is a matrix, then sum(A) returns a row vector containing the sum
of each column.
So your next action:
mean_train = 1/(num_train )* sum(Xtrain);
returns 1x2 array and matlab can't make this: Xtrain - mean_train.
Answer is easy:
use sum twice like this: sum(sum(Xtrain))
So im using MATLAB to calculate the coefficient of an equation and it keeps displaying fractions as the constants instead of decimal form like xxe-x
My code is below
bt = 0.03175;
bpzt = 0.0078;
ht = 0.003;
w = 50; % frequency in Hz
pnic = 8908; % density of nickel
Enic = 200e9; % Young's modulus of nic
L = 0.3048; % Length of canitlever beam in m
syms hn
inertia = (1/12)*bt*ht^3 + (1/2)*bpzt*ht^2*hn - (1/2)*bpzt*ht*hn^2 + (2/3)*bpzt*hn^3
area = (bt*ht - 2*hn*bt + 2*bpzt*hn);
You are using the symbolic math toolbox. You have to convert any symbolic variable back to a numerical one. Use double(area) to get a double value of your symbolic variable area. You can use the function single, too.
But be aware that hn has to get a value first, otherwise it cannot be determined.
Documentation: http://www.mathworks.com/help/symbolic/double.html
To format this output, use the usual Matlab tools. In your case format short e
More details: http://www.mathworks.com/help/matlab/matlab_prog/display-format-for-numeric-values.html
In R2014b, this line:
inertia = (1/12)*bt*ht^3 + (1/2)*bpzt*ht^2*hn - (1/2)*bpzt*ht*hn^2 + (2/3)*bpzt*hn^3
returns
(13*hn^3)/2500 - (863307622649607*hn^2)/73786976294838206464 + (5304162033559185*hn)/151115727451828646838272 + 5527208847278085/77371252455336267181195264
which is a symbolic expression with numeric values represented exactly as rational fractions (even though they may have started out as decimal values in your code). You can convert this using vpa
vpa(inertia)
which returns:
0.0052*hn^3 - 0.000011699999999999999788190263583232*hn^2 + 0.000000035099999999999996664653271376613*hn + 0.000000000071437500000000005341045287673881
The length/precision of the decimal values depends on digits. Displaying this in an exponential-style format (xxe-x) is not an option for any symbolic math expression or value unless you write your own function to parse the string and do the conversion.
To convert this to a vectorized double-precision floating point function, you can use the terribly-named matlabFunction – matlabFunction(inertia) returns:
#(hn)hn.*3.51e-8-hn.^2.*1.17e-5+hn.^3.*5.2e-3+7.143750000000001e-11
But if you're doing this, I'd wonder why you were working with symbolic math in the first place and if it's possible to do everything in much faster double precision.
Keep in mind that if you want to convert to any kind of decimal form, variable precision or floating-point, in many cases you will lose precision. However, if you just want to view the result or perform fourth calculations if double precision, then it should be fine.
I have a symbolic and numeric mixed expression:
(3145495418313256125*sin(11334310783410932061962315977/17437937757178560512000000000)*cos(theta))/85568392920039424
where theta is a symbolic variable. I want to simplify this expression such that all the numeric numbers and their math operation results are changed to double.
In terms of data types, you can't mix floating point and symbolic values. However, you can use variable precision arithmetic so that the values are represented in decimal form. Using vpa:
syms theta
y1 = (3145495418313256125*sin(11334310783410932061962315977/17437937757178560512000000000)*cos(theta))/85568392920039424
y2 = vpa(y1)
which returns
y2 =
22.24607614528243677915796931637*cos(theta)
The data type (class) of y2 is still sym. See the digits function to adjust the number of significant digits.
If you want to work in actual floating point you'll need to convert your symbolic expression into a function. You can automate that procedure by using the confusingly-named matlabFunction:
thetafun = matlabFunction(y1)
which returns a function using double precision variables:
thetafun =
#(theta)cos(theta).*2.224607614528244e1
The anonymous function thetafun can then be called just like any function, e.g., thetafun(0.5).
You can make use of coeffs command to achieve the desired:
f=2*cos(theta)/3+5*sin(theta)/19
c_f=coeffs(f);
fraction_c_f=double(c_f);
ans = [0.2632 0.6667]
I have a problem with symbolic functions. I am creating function of my own whose first argument is a string. Then I am converting that string to symbolic function:
f = syms(func)
Lets say my string is sin(x). So now I want to calculate it using subs.
a = subs(f, 1)
The result is sin(1) instead of number.
For 0 it works and calculates correctly. What should I do to get the actual result, not only sin(1) or sin(2), etc.?
You can use also use eval() to evaluate the function that you get by subs() function
f=sin(x);
a=eval(subs(f,1));
disp(a);
a =
0.8415
syms x
f = sin(x) ;
then if you want to assign a value to x , e.g. pi/2 you can do the following:
subs(f,x,pi/2)
ans =
1
You can evaluate functions efficiently by using matlabFunction.
syms s t
x =[ 2 - 5*t - 2*s, 9*s + 12*t - 5, 7*s + 2*t - 1];
x=matlabFunction(x);
then you can type x in the command window and make sure that the following appears:
x
x =
#(s,t)[s.*-2.0-t.*5.0+2.0,s.*9.0+t.*1.2e1-5.0,s.*7.0+t.*2.0-1.0]
you can see that your function is now defined by s and t. You can call this function by writing x(1,2) where s=1 and t=1. It should generate a value for you.
Here are some things to consider: I don't know which is more accurate between this method and subs. The precision of different methods can vary. I don't know which would run faster if you were trying to generate enormous matrices. If you are not doing serious research or coding for speed then these things probably do not matter.
How can I make a function from a symbolic expression? For example, I have the following:
syms beta
n1,n2,m,aa= Constants
u = sqrt(n2-beta^2);
w = sqrt(beta^2-n1);
a = tan(u)/w+tanh(w)/u;
b = tanh(u)/w;
f = (a+b)*cos(aa*u+m*pi)+a-b*sin(aa*u+m*pi); %# The main expression
If I want to use f in a special program to find its zeroes, how can I convert f to a function? Or, what should I do to find the zeroes of f and such nested expressions?
You have a couple of options...
Option #1: Automatically generate a function
If you have version 4.9 (R2007b+) or later of the Symbolic Toolbox you can convert a symbolic expression to an anonymous function or a function M-file using the matlabFunction function. An example from the documentation:
>> syms x y
>> r = sqrt(x^2 + y^2);
>> ht = matlabFunction(sin(r)/r)
ht =
#(x,y)sin(sqrt(x.^2+y.^2)).*1./sqrt(x.^2+y.^2)
Option #2: Generate a function by hand
Since you've already written a set of symbolic equations, you can simply cut and paste part of that code into a function. Here's what your above example would look like:
function output = f(beta,n1,n2,m,aa)
u = sqrt(n2-beta.^2);
w = sqrt(beta.^2-n1);
a = tan(u)./w+tanh(w)./u;
b = tanh(u)./w;
output = (a+b).*cos(aa.*u+m.*pi)+(a-b).*sin(aa.*u+m.*pi);
end
When calling this function f you have to input the values of beta and the 4 constants and it will return the result of evaluating your main expression.
NOTE: Since you also mentioned wanting to find zeroes of f, you could try using the SOLVE function on your symbolic equation:
zeroValues = solve(f,'beta');
Someone has tagged this question with Matlab so I'll assume that you are concerned with solving the equation with Matlab. If you have a copy of the Matlab Symbolic toolbox you should be able to solve it directly as a previous respondent has suggested.
If not, then I suggest you write a Matlab m-file to evaluate your function f(). The pseudo-code you're already written will translate almost directly into lines of Matlab. As I read it your function f() is a function only of the variable beta since you indicate that n1,n2,m and a are all constants. I suggest that you plot the values of f(beta) for a range of values. The graph will indicate where the 0s of the function are and you can easily code up a bisection or similar algorithm to give you their values to your desired degree of accuracy.
If you broad intention is to have numeric values of certain symbolic expressions you have, for example, you have a larger program that generates symbolic expressions and you want to use these expression for numeric purposes, you can simply evaluate them using 'eval'. If their parameters have numeric values in the workspace, just use eval on your expression. For example,
syms beta
%n1,n2,m,aa= Constants
% values to exemplify
n1 = 1; n2 = 3; m = 1; aa = 5;
u = sqrt(n2-beta^2);
w = sqrt(beta^2-n1);
a = tan(u)/w+tanh(w)/u;
b = tanh(u)/w;
f = (a+b)*cos(aa*u+m*pi)+a-b*sin(aa*u+m*pi); %# The main expression
If beta has a value
beta = 1.5;
eval(beta)
This will calculate the value of f for a particular beta. Using it as a function. This solution will suit you in the scenario of using automatically generated symbolic expressions and will be interesting for fast testing with them. If you are writing a program to find zeros, it will be enough using eval(f) when you have to evaluate the function. When using a Matlab function to find zeros using anonymous function will be better, but you can also wrap the eval(f) inside a m-file.
If you're interested with just the answer for this specific equation, Try Wolfram Alpha, which will give you answers like:
alt text http://www4c.wolframalpha.com/Calculate/MSP/MSP642199013hbefb463a9000051gi6f4heeebfa7f?MSPStoreType=image/gif&s=15
If you want to solve this type of equation programatically, you probably need to use some software packages for symbolic algebra, like SymPy for python.
quoting the official documentation:
>>> from sympy import I, solve
>>> from sympy.abc import x, y
Solve a polynomial equation:
>>> solve(x**4-1, x)
[1, -1, -I, I]
Solve a linear system:
>>> solve((x+5*y-2, -3*x+6*y-15), x, y)
{x: -3, y: 1}