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
Related
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.
I have to solve an inequality but it is too hard to do it by hand. Therefore, I would like to use Matlab. Let a = [(k-3)*sqrt(v)]/s and b = 1.08148*a^2-epsilon, where epsilon = 10^(-6). The inequality that needs to be solved is:
q(a,b) < s*sqrt(v)
where s and v are known. More precisely I want to solve the above inequality for k (which occurs in a and b). Now, the problem is that q(a,b) is the greatest real root of the quartic polynomial:
(48*a^2+16*b)*x^4 - (40*a^3+168*a*b)*x^3+(-45*a^4+225*a^2*b+72*b)*x^3+(27*a^2*b-162*a*b^2)*x+27*b^3
I tried to run this:
syms x z
av = ((x-3)*sqrt(v))/s;
Q = max(roots([48*z^2+16*(1.08148*z-eps), -40*z^3-168*z*(1.08148*z-eps), -45*z^4+225*z^2*(1.08148*z-eps)+72*(1.08148*z-eps)^2, 27*z^3*(1.08148*z-eps)-162*z*(1.08148*z-eps)^2, 27*(1.08148*z-eps)^3]));
F = compose(Q,av);
solve(F-skew*sqrt(var)<0, x)
but Matlab keeps giving the following error:
Error using sym/max (line 97)
Input arguments must be convertible
to floating-point numbers.
Error in Testt (line 13)
R = max(roots([2048,
-6912*((x-3)sqrt(var)/skew)^2,
8088((x-3)sqrt(var)/skew)^4,
-3600((x-3)sqrt(var)/skew)^6,
375((x-3)*sqrt(var)/skew)^8]));
Perhaps someone has a better idea to solve it? The best way would be if I had an explicit expression for the greatest real root q of the quartic in function of a and b. However, this explicit expression is too lengthy to use.
The error information seemed to say the max() function do not support sym x z.
Because the returned result of roots() includes x and z.But Matlab doesn't know the value of x and z,so it can't calculate the value of the roots and then can't compare them.
Maybe you should improve your algorithm.
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.
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
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)