Here is a function handler to the function of interest:
fun = #(x) 1 / (sqrt(x) * (x + 1));
q = integral(fun, 0, inf)
But I got the following error:
Error using *
Inner matrix dimensions must agree.
How can I solve this?
You have to use . periods when defining an element-wise operation rather than matrix operations. This is needed for the integral function inputs.
Your function should be
fun = #(x) 1./(sqrt(x).*(x+1));
Giving
q = integral(fun,0,inf)
q =
3.1416
This overview article on Array vs. Matrix operations may be of interest, outlining all element-wise . type functions.
https://uk.mathworks.com/help/matlab/matlab_prog/array-vs-matrix-operations.html
Specifically, see these help articles on multplication and right-array division to give you an idea: https://www.mathworks.com/help/matlab/ref/times.html, https://www.mathworks.com/help/matlab/ref/rdivide.html
Related
This question already has an answer here:
Matlab - Incorrect dimensions for raising a matrix to a power [closed]
(1 answer)
Closed 2 years ago.
Here is my code:
x = 60: 95;
r11 = 0.93;
E1=13.5;
F1=0.00529;
G1=0;
H1=1;
k11=60;
k12=0;
uid = 10^-7*1.31275 * exp(0.145961 * x );
P = exp(-F1 * ((x-k11)^2) - G1*(x-k12));
fi= E1 * P + H1;
y1 = r1 * fi * uid;
plot(x,y1);
My error is:
Error using ^ (line 51)
Incorrect dimensions for raising a matrix to a power. Check that the matrix is square and the power is a scalar. To perform elementwise matrix powers, use '.^'.
Error in ui12 (line 16)
P = exp(-F1 * ((x-k11)^2) - G1*(x-k12));
The error refers to the snippet (x-k11)^2, where you subtract a scalar (k11) from an array (x, 1x36) and try to square the result. The problem is that the function ^ is the shortcut for the function mpower(), which is the matrix-power function and consequently expects a scalar or a matrix calculation as it essentially is
x^2 == x*x
x^3 == x*x*x
However, it does not know what to do with an array as x*x does not work (try do run rand(1,36)*rand(1,36), which will essentially raise the same error).
It also suggests a solution: .^, which is the element-wise power function (in fact, the . in an arithmetic operation usually indicates that the following operation is conducted element-wise). This .^ is the shortcut for the "normal" power function as you would have expected in the first place. It performs the ^2 to every element of the array x.
x.^2 == power(x,2)
extended side note:
To mimic the behavior of the element-wise operator ., you may want to have a look at the arrayfun function, which applies a certain function to every element of a matrix or array/vector. If you are a new with matlab (as I assume from your question), this hint may just confuse you.
x.^2 == arrayfun(#(a)mpower(a,2),x)
I would love to know why this code is not working. if anyone has any information on what is causing matlab to find so many errors, it would be greatly appreciated.
m = 1;
c = 1.5;
fun =#(x, epsilon) 1 .* (1 - (1 - cos(x))/(2.*epsilon)).^c .* cos(m.*x);
a = #(ep) acos(1-(2*ep));
lm =#(e) 1/(2.*pi) .* integral(#(x)fun(x, e), -1.*a(e), a(e));
fprintf('ball bearing at 0.6 is %4.4f', lm(0.6));
the function that I am trying to replicate is 𝐼𝑚(𝜀) =1/2𝜋∫[1 − (1 − cos(𝑥))/2𝜀]^𝑐 cos(𝑚𝑥)dx
There should be no need for the dot modifier on the multiplication to my knowledge, however Matlab was complaining that this required element-wise operations even though there are no matrices involved.
According to the documentation of integral the function to be integrated must be vectorized:
For scalar-valued problems, the function y = fun(x) must accept a vector argument, x, and return a vector result, y. This generally means that fun must use array operators instead of matrix operators. For example, use .* (times) rather than * (mtimes).
Is it possible that meijerG function contain a negative value (i.e. is {-1,0,0})? I tried both Mathematica and Matlab to compute this meijerG function but they generate an error that this meijerG is not defined for the given parameters`.
Here is my code:
D = (0.6);
lg1 = lg2 = 1;
G = evalin(symengine, sprintf('meijerG([[0], []], [[-1,0,0], []],%f)',(D/(lg1*lg2))));
CD = -((2*D)/(lg1*lg2*(log(4))))*G;
Here I have also attached the image of the function from the text.
From the documentation of meijerG:
No pair of parameters ai - bj, i = 1, …, n. j = 1, …, m, should differ by a positive integer [...] . Otherwise, meijerG returns an error.
Complex numbers are valid for any coefficent; however in you case you have a0-b0 = 1 which is forbidden.
I quickly looked at that. If one expand log2(1+x) into Taylor series, substitute \gamma->x^2, then integral would be
S K0(x) x^m dx = 2^(m-1) G((m+1)/2)^2
see here for details. G is gamma-function and for argument like (k+1/2) it is expressed via binomial coeff times sqrt(\pi), see here for details.
After all that you have infinite sum of terms with polynomials over lambdas and b and some binomial coefs and \pi etc. Whether it could be summed or not - I don't know...
I'm trying to implement this integral representation of Bessel function of the first kind of order n.
here is what I tried:
t = -pi:0.1:pi;
n = 1;
x = 0:5:20;
A(t) = exp(sqrt(-1)*(n*t-x*sin(t)));
B(t) = integral(A(t),-pi,pi);
plot(A(t),x)
the plot i'm trying to get is as shown in the wikipedia page.
it said:
Error using * Inner matrix dimensions must agree.
Error in besselfn (line 8) A(t) = exp(sqrt(-1)*(n*t-x*sin(t)));
so i tried putting x-5;
and the output was:
Subscript indices must either be real positive integers or logicals.
Error in besselfn (line 8) A(t) = exp(sqrt(-1)*(n*t-x*sin(t)));
How to get this correct? what am I missing?
To present an anonymous function in MATLAB you can use (NOT A(t)=...)
A = #(t) exp(sqrt(-1)*(n*t-x.*sin(t)));
with element-by-element operations (here I used .*).
Additional comments:
You can use 1i instead of sqrt(-1).
B(t) cannot be the function of the t argument, because t is the internal variable for integration.
There are two independent variables in plot(A(t),x). Thus you can display plot just if t and x have the same size. May be you meant something like this plot(x,A(x)) to display the function A(x) or plot(A(x),x) to display the inverse function of A(x).
Finally you code can be like this:
n = 1;
x = 0:.1:20;
A = #(x,t) exp(sqrt(-1)*(n*t-x.*sin(t)));
B = #(x) integral(#(t) A(x,t),-pi,pi);
for n_x=1:length(x)
B_x(n_x) = B(x(n_x));
end
plot(x,real(B_x))
I have the equation 1 = ((π r2)n) / n! ∙ e(-π r2)
I want to solve it using MATLAB. Is the following the correct code for doing this? The answer isn't clear to me.
n= 500;
A= 1000000;
d= n / A;
f= factorial( n );
solve (' 1 = ( d * pi * r^2 )^n / f . exp(- d * pi * r^2) ' , 'r')
The answer I get is:
Warning: The solutions are parametrized by the symbols:
k = Z_ intersect Dom::Interval([-(PI/2 -
Im(log(`fexp(-PI*d*r^2)`)/n)/2)/(PI*Re(1/n))], (PI/2 +
Im(log(`fexp(-PI*d*r^2)`)/n)/2)/(PI*Re(1/n)))
> In solve at 190
ans =
(fexp(-PI*d*r^2)^(1/n))^(1/2)/(pi^(1/2)*d^(1/2)*exp((pi*k*(2*i))/n)^(1/2))
-(fexp(-PI*d*r^2)^(1/n))^(1/2)/(pi^(1/2)*d^(1/2)*exp((pi*k*(2*i))/n)^(1/2))
You have several issues with your code.
1. First, you're evaluating some parts in floating-point. This isn't always bad as long as you know the solution will be exact. However, factorial(500) overflows to Inf. In fact, for factorial, anything bigger than 170 will overflow and any input bigger than 21 is potentially inexact because the result will be larger than flintmax. This calculation should be preformed symbolically via sym/factorial:
n = sym(500);
f = factorial(n);
which returns an integer approximately equal to 1.22e1134 for f.
2. You're using a period ('.') to specify multiplication. In MuPAD, upon which most of the symbolic math functions are based, a period is shorthand for concatenation.
Additionally, as is stated in the R2015a documentation (and possibly earlier):
String inputs will be removed in a future release. Use syms to declare the variables instead, and pass them as a comma-separated list or vector.
If you had not used a string, I don't think that it would have been possible for your command to get misinterpreted and return such a confusing result. Here is how you could use solve with symbolic variables:
syms r;
n = sym(500);
A = sym(1000000);
d = n/A;
s = solve(1==(d*sym(pi)*r^2)^n/factorial(n)*exp(-d*sym(pi)*r^2),r)
which, after several minutes, returns a 1,000-by-1 vector of solutions, all of which are complex. As #BenVoigt suggests, you can try the 'Real' option for solve. However, in R2015a at least, the four solutions returned in terms of lambertw don't appear to actually be real.
A couple things to note:
MATLAB is not using the values of A, d, and f from your workspace.
f . exp is not doing at all what you wanted, which was multiplication. It's instead becoming an unknown function fexp
Passing additional options of 'Real', true to solve gets rid of most of these extraneous conditions.
You probably should avoid calling the version of solve which accepts a string, and use the Symbolic Toolbox instead (syms 'r')