I want to make matlab agree with the following simple statement:
(x-a)^b>=0 when x>0, a>0, b<0, x>a
In order to do so I write:
syms x a b
assume(x>0 & a>0 & b<0 & x>a);
isAlways((x-a)^b>=0,'Unknown','error')
And get the following error message:
Error using symengine
Cannot prove '0 <= (x - a)^b'.
Error in sym/isAlways (line 38)
Y = mupadmex('symobj::isAlways',X.s,isMath,['"Unknown' p.Unknown '"'],9);
Why does MATLAB fail to agree with such an evident statement?
I don't know why but instead of using as assumption x>a, use x-a > 0. I think MATLAB doesn't "compute" the difference between x and a and so it doesn't know if x-a is greater than 0 or not. So it must already know that x-a is greater than 0 when you check the condition.
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 need to find inverse of virtual value function of lognormal random variables. This is what I tried to do:
syms flogn(x,p1,p2)
% assume(p2<=0); %Adding this doesn't change the error
flogn(x,p1,p2) = x - logncdf(x,p1,p2,'upper')/lognpdf(x,p1,p2);
glogn = finverse(flogn,x);
But I got the error:
Error using symengine
Unable to prove 'p2 <= 0' literally. Use 'isAlways' to test the statement mathematically.
Error in sym/subsindex (line 810)
X = find(mupadmex('symobj::logical',A.s,9)) - 1;
Error in sym/privsubsasgn (line 1085)
L_tilde2 = builtin('subsasgn',L_tilde,struct('type','()','subs',{varargin}),R_tilde);
Error in sym/subsasgn (line 922)
C = privsubsasgn(L,R,inds{:});
Error in logncdf>locallogncdf (line 73)
sigma(sigma <= 0) = NaN;
Error in logncdf (line 47)
[varargout{1:max(1,nargout)}] = locallogncdf(uflag,x,varargin{:});
Error in Untitled5 (line 3)
flogn(x,p1,p2) = x - logncdf(x,p1,p2,'upper')/lognpdf(x,p1,p2);
I also tried with beta distribution, and got a similar error. How can I use logncdf with symbolic variables?
Note that you could have minimalize your sample code even more, like this:
syms flogn(x,p1,p2)
flogn(x,p1,p2) = logncdf(x,p1,p2);
It's shorter, produces the same error and helps us to focus on the source of the error message.
So, the error doesn't come from trying to inverse a function, but from trying to use an existing function for numerical calculations with symbolic variables.
The error comes from the fact that you want to create a symbolic function flogn based on existing function logncdf, and logncdf has a multiple comparisons.
With the command edit logncdf, you can read the source code of the function and see comparison at lines 73 and 76.
% Return NaN for out of range parameters.
sigma(sigma <= 0) = NaN;
% Negative data would create complex values, which erfc cannot handle.
x(x < 0) = 0;
Matlab cannot compare symbols so it throws errors.
Depending on what you really need, you can have different solutions.
Do you really need to symbolize the function flogn? Couldn't you just write it as a function then calculate the inverse of it (if it can be inversed...)?
If you really want to keep the symbolization, you can also rewrite your own function logncdf (with another name) so it does not have the comparisons. But it's still not guaranteed that you will find an inverse.
I just started learning MATLAB and I'm trying to normalize a bump function given by
function b = bump(x)
region1 = abs(x) < 1
b(region1) = (exp(-1./(1 - x(region1).^2)))
region2 = abs(x) >= 1
b(region2) = 0
end
To do this, I need to divide by the definite integral from -1 to 1. However, when I input
syms x;
int(bump(x), -1, 1)
I get a long error message, which says
Error using symengine (line 58)
Unable to prove 'abs(x) < 1' literally. To test the statement mathematically, use isAlways.
Error in sym/subsindex (line 1554)
X = find(mupadmex('symobj::logical',A.s,9)) - 1;
Error in sym>privformat (line 2357)
x = subsindex(x)+1;
Error in sym/subsref (line 1578)
[inds{k},refs{k}] = privformat(inds{k});
Error in bump (line 3)
b(region1) = (exp(-1./(1 - x(region1).^2)))
I tried replacing abs(x)<1 with what I think is the suggested isAlways(abs(x)<1), and that removes the error, although it gives the wrong answer (it says the integral is zero).
I don't understand what does the error message means.
syms x defines x as a symbolic variable, invoking symbolic computation on x. This probably isn't what you want.
Instead, define x as some kind of input matrix, e.g. x = zeros(3);. Or, to do numeric integration, use the integral function:
integral(#bump, -1, 1)
I am trying to find the value x for a function f(x,y) that produces the function value 0 for a given y. In Matlab I write a small function handle, e.g.
minme = #(y,x) y-x.^2;
and use the fzero function to find that value of x, call it x*.
So e.g.
fzero(#(x) minme(5,x),1)
works great. However, now I want to find x* for a large vector of values of y, called Y. Putting
minme(Y,x)
for some value of x works.
Now I was trying something like
fzero(#(x) minme((3:1:5),x),1)
and
fzero(#(x) minme(Y,x),1)
but that produces an error:
??? Operands to the || and && operators must be convertible to logical scalar values.
Error in ==> fzero at 333
elseif ~isfinite(fx) || ~isreal(fx)
Does anybody know whether there is a way to do this?
Check this out
arrayfun(#(i) fzero(#(x) minme(y(i),x),1),1:numel(y))
Disclaimer. I am familiar with Mathematica but not Matlab, so I apologize if this is a neophyte question.
I am getting a strange error on Matlab on the the following at using Matlab's solve command:
solve(0.2 = (1.4+1/2)^((1.4+1)/(2*(1.4-1)))*(M)/((1+(1.4-1)/2*M^2))^((1.4+1)/(2*(1.4-1))), M)
The error is:
Error: The expression to the left of the equals sign is not a valid target for an assignment.
The equivalent Solve command in Mathematica (using the same expression) works perfectly, so I don't think my expression itself is invalid.
Moreover, I get the same error when I try to use the examples on the doc site: http://www.mathworks.com/help/symbolic/mupad_ref/solve.html
Is it a configuration problem or is there something about the syntax of the command I am misinterpreting?
Edit: I also tried with == instead of =, and I get a different error:
Undefined function or variable 'M'.
Also, as a note, I am running Matlab R2011b (7.13.0.564) 64-bit (glnxa64).
Edit2: I tried the first suggested solution with syms:
>> syms M
>> solve(0.2 == (1.4+1/2)^((1.4+1)/(2*(1.4-1)))*(M)/((1+(1.4-1)/2*M^2))^((1.4+1)/(2*(1.4-1))), M)
Error using char
Conversion to char from logical is not possible.
Error in solve>getEqns (line 245)
vc = char(v);
Error in solve (line 141)
[eqns,vars,options] = getEqns(varargin{:});
Edit3: I have been able to reproduce this issue with even the simplest of equations
>> syms x
>> solve(x^2 -4 == 0, x)
Error using char
Conversion to char from logical is not possible.
Error in solve>getEqns (line 245)
vc = char(v);
Error in solve (line 141)
[eqns,vars,options] = getEqns(varargin{:});
Moreover, I tried the solution suggested here too: MATLAB examples are failing
Matlab's fsolve command assumes the expression is set to zero. If solving numerically, you would not do:
x=solve(2=x+1,x)
but rather:
x=fsolve(#(x) x+1-2,0)
Where the equation is already set to zero, #(x) is what you are solving for, and 0 is the initial guess. Which you must include.
Using solve symbolically, it looks like this:
syms x
val=solve(x+1-2)
Or for your system:
syms M
solve(-0.2+ (1.4+1/2)^((1.4+1)/(2*(1.4-1)))*(M)/((1+(1.4-1)/2*M^2))^((1.4+1)/(2*(1.4-1))))
ans =
4.7161724968093348297842999805458
0.029173662296926424763929809009225
- 3.8716404782846254923900841980317 - 3.4984412176176158766571497649425*i
1.4989673987314948651159693032542 + 5.5784387926679222829321168661041*i
1.4989673987314948651159693032542 - 5.5784387926679222829321168661041*i
- 3.8716404782846254923900841980317 + 3.4984412176176158766571497649425*i
you should define M as sym, and use == instead of =
syms M
solve(0.2 == (1.4+1/2)^((1.4+1)/(2*(1.4-1)))*(M)/((1+(1.4-1)/2*M^2))^((1.4+1)/(2*(1.4-1))), M)
ans =
4.7161724968093348297842999805458
0.029173662296926424763929809009225
- 3.8716404782846254923900841980317 - 3.4984412176176158766571497649425*i
1.4989673987314948651159693032542 + 5.5784387926679222829321168661041*i
1.4989673987314948651159693032542 - 5.5784387926679222829321168661041*i
- 3.8716404782846254923900841980317 + 3.4984412176176158766571497649425*i