Error using integral: A and B must be floating-point scalars - matlab

I want to evaluate the simple example of integral
a = max(solve(x^3 - 2*x^2 + x ==0 , x));
fun = #(x) exp(-x.^2).*log(x).^2;
q = integral(fun,0,a)
and the error is
Error using integral (line 85)
A and B must be floating-point scalars.
Any tips? The lower limit of my integral must be a function, not a number.

The Matlab command solve returns symbolic result. integral accepts only numeric input. Use double to convert symbolic to numeric. As your code is written now, already max should throw an error due to symbolic input. The following works.
syms x;
a = max(double(solve(x^3 - 2*x^2 + x)));
fun = #(x) exp(-x.^2).*log(x).^2;
q = integral(fun,0,a)
Output: 1.9331.
the lower limit of my integral must be a function, not a number
integral is a numeric integration routine; the limits of integration must be numeric.

Check values of a by mouse over in breakpoint or removing the ; from the end of the line so it prints a. Based on the error, a is not a scalar float. You might need another max() or double() statement to transform the vector to a single value.
Solve Help : http://www.mathworks.com/help/symbolic/solve.html
Integral Help : http://www.mathworks.com/help/ref/integral.html

Related

How to perform implicit derivative?

I'm going to perform some symbolic calculus for a project and I'm starting with something simple.
I'm ttrying to calculate the derivative of L function in respect to variable fi, but using the following code I get the error shown.
syms x(t) y(t) fi(t) m IR t;
L = 1/2*(m*(diff(x(t),t)^2+diff(y(t),t)^2) + IR*diff(fi(t),t)^2)
D1 = diff(L,diff(fi(t),t))
ERROR:
Error using sym/diff (line 70)
Second argument must be a variable or a nonnegative integer specifying the number of differentiations.
Could anyone tell me what's going on? Thanks.
[1]: https://i.stack.imgur.com/fTpEM.png
You have a nested diff in the second line. For the outer diff, the second argument cannot be the inner diff - it should be a variable or non-negative integer.

Why does Matlab factorial function perceives an integer as a non-integer?

I'm trying to build a function in Matlab which generates a Taylor series around 0 for sine and the user can insert x (the value for which the sine is approximated) and a maximum error. Thus I want the function to check the maximum error and from this maximum it generates the amount of elements in the Taylor series.
I get the following error:
Error using factorial (line 20)
N must be an array of real non-negative integers.
Error in maxError (line 19)
y(x) = y(x) + (-1)^(j) * x^(2j+1)/factorial(2j+1)
Below my code.
function [n] = maxError(x,e);
%Computes number of iterations needed for a given absolute error.
n=1;
while abs(x)^(n+1)/factorial(n+1) >= e
n = n+1;
end
if mod(n,2) == 0
n=n+1;
end
y=#(x) x;
j=1;
while j<n
y(x) = y(x) + (-1)^(j) * x^(2j+1)/factorial(2j+1)
j=j+1;
end
return
I think I get the error because the factorial function can only take up integers, but the way I see it, I am feeding it an integer. Since j=1; and then gets larger by one per iteration, I don't see how Matlab can perceive this as something else than a integer.
Any help is appreciated.
You are using j as an indexing variable, which is also the complex number in Matlab, and your are forgetting a * multiply.
You can use j as a variable (not recommended!) but when you are putting a number in front of it, Matlab will stil interpret is as the complex number, and not as the variable.
Adding the multiplication symbol will solve the issue, but using i and j as variables will give you these hard to debug errors. If you had used a, the error would have been easier to understand:
>> a=10;
>> 2a+1
2a+1
↑
Error: Invalid expression. Check for missing multiplication operator, missing or
unbalanced delimiters, or other syntax error. To construct matrices, use brackets
instead of parentheses.

symsum function error - invalid indexing or function definition?

I'm attempting to sum the following using the symsum function in MATLAB:
sum (from q=0 to 5) [a(q+1)*x(2)^q]
where a=[a0, a1, ..., a5], x=[x(1), x(2), ...] where x(1), x(2), ... are scalars.
The sum is a0 + a1x(2)+a2x(2)^2 +...+a5x(2)^5.
I've used the following code:
syms q a x
f=a(q+1)*x(2)^q
symsum(f, q, 0, 5)
where x(2)= -4.9.
However, the above code returns "Invalid indexing or function definition".
Using f=x(2)^q does not result in the error, however, using f=a(q+1) does return the error. Therefore the problem lies within the a(q+1) term.
Any help is greatly appreciated!
It doesn't matter a is matrix of symbolics or non-symbolics.
Every call to a matrix need an index(a number, real number like 1,2,3,4,...)
When q is a symbolic it means q really doesn't equal to any number, so a(q)
has no meaning because q has no equality to any number. So programs make error and stops before going more.
>>a=0:5;
>> a(1)
ans =
0
>>a(q) % ?-->do you know the real value of `q` right now? NO,nobody knows!
MAKES ERROR

double integral implementation in matlab

I am in trouble to implement the following double integral. there is a summation inside the integral which make things a bit complicated. The matlab code I did is as follows and always has error like "Matrix dimensions must agree." , any hint to implement it? thanks
n=3;
nn=1:n;
aa=gamma([1:n])
thre=3;
lapha=4;
r=3;
fun1= #(theta, x) (1-sum( lambda *pi *( (x-r).^2+r^2-(x-r).*r.*cos(theta)).^(nn-1)./aa).*exp(-1*lambda *pi*((x-r).^2+r^2-(x-r).*r.*cos(theta)))).*lambda/n*(1-1/2^n).*thre.*r.^alpha.*(x-r).^(1-alpha) ;
answer=integral2( fun1, 0, 2*pi, 0, inf )
The double integral:
Your problem is the way you calculate the sum in the integrated function. The documentation of intergal2 says that the function argument must accept arrays X and Y of the same size and return an array of corresponding values. But this expression inside the function definition:
((x-r).^2 + r^2 - (x-r).*r.*cos(theta)).^(nn-1)./aa
is not working the way you expect, because it's you who decide the size of nn and aa, while it's integral2 that decides the size of the vectors x and theta; no wonder that there are disagreements on it.

why isn't the result a scalar?

i'm stuck with this error:
In an assignment A(I) = B, the number of elements in B and I must be the same.
yres(1)=((u - uc).^2) + ((y - yc).^2) -(d.^2);
i don't understand, why this won't get a skalar?since the elements are all scalar. what should be changed to get a scalar?
best regards
edit: thanks sloede, all inputs are scalar, but i still get this error
In an assignment A(I) = B, the number of elements in B and I must be the
same.
Error in myfun (line 7)
yres(1)=sqrt(((u - uc).^2) + ((y - yc).^2) ) -d;
Error in fsolve (line 241)
fuser = feval(funfcn{3},x,varargin{:});
Error in modfsolve (line 26)
x= fsolve(#myfun,x0,options,uc,d,spacing_amplitude,spacing_width);
Caused by:
Failure in initial user-supplied objective function evaluation. FSOLVE
cannot continue.*
The "." before an operator means that the following operation should be applied element-wise and not on the vector as a whole. Thus
a = b.^2
will give you as a result all elements of b squared and saved back to a. Therefore, in your code statement above, if any of u, uc, y, yc, d are not scalar but a vector, your result will be a vector as well.
Otherwise there seems to be nothing wrong with your code.
read the documentation of fsolve: http://www.mathworks.nl/help/toolbox/optim/ug/fsolve.html
it states:
fun
The nonlinear system of equations to solve. fun is a function that accepts a vector x and returns a vector F, the nonlinear equations evaluated at x.
Obviously your function myfun doesn't handle vector input.
You can solve this by adding the following construction inside your function (and of course change it to your needs/your parameters):
function out = myfun(in)
if ~isscalar(in)
% assuming it's a matrix or vector
out = reshape(arrayfun(#myfun,in(:)),size(in));
else
% your actual function execution statements
out = dostuffon(in);
end
end
or properly vectorize your function (if that's possible)