In my old 2007 Matlab code, I was doing modulo operation on symbolic varaibles as follows:
syms x
if (mod(5*x, x) == 0)
in = 1
end
Today I ran the same program in R2013a and I am getting
mod(5*x, x)= x*(5 mod x)
Due to this result, the if condition is not passed. As a result, my program is aborting.
I am not able to understand the reasons for this. I appreciate your comments.
Thanks in advance.
Related
I am trying to find use to Newton-Raphson method to find the roots. It does this by making a guess and then improving the guess after each iteration until you get one of the zeros.
Because the Newton-Raphson method quickly finds the zeros, it gives me a small error immediately and after two or three iterations max it should fail to meet the conditions of the while loop. However, the problem is that when I remove the semi-colon after "error" in my loop, I start getting fractions that should break the while loop, but its like Matlab doesn't know that 123/8328423 is less than 1. It continues to run until I manually force the program to stop running.
How do I fix this? I tried format long, format longe, and using double both in the command window, in the scrip file, and somewhere in the loop.
Thank you in advance for any tips, suggestions, or advice that may help!!
A = [1,2,-4;2,-2,-2;-4,-2,1;];
format longe
% syms x y z
% P = x^4 + 3*x^2*y^2-z^3+y+1;
% feval(symengine,'degree',P,x)
syms x
B = mateigenvalue(A);
f(x) = simplify(matdet(B));
x0 = 1;
error = 10;
while(error > .01)
x1 = x0 - f(x0)/(27*(x0)-3*(x0)^2);
error = abs(((f(x0)-f(x1))/f(x0))*100)
x0 = x1;
end
x0 = double(x0)
I reckon the main problem is with error.
It starts as double but inside the while-loop it turns into a symbolic variable and you can't easily compare symbolic variables with scalar values (the .01 in the while-loop condition).
Check in your workspace if error is symbolic (or type class(error) and check if sym is returned). I guess it is symbolic because a fraction is returned (123/8328423), as instead Matlab treats double values with decimals, not fractions.
If so, try doing (inside the while-loop) a conversion for error that is, under the line
error = abs(((f(x0)-f(x1))/f(x0))*100);
try putting
error=double(error);
So error will be temporarily converted in double and you can easily compare its value with .01 to check the while-loop condition.
Also, it is bad practice to call a variable error since error() is a built-in function in Matlab. By naming a variable error you cannot use the error() function. Same story goes for other built-in functions.
I'm trying to write some code which would find the limit of a function as x approaches positive and negative infinity. The code I have so far is as follows:
pos = limit(exp(atan(x)), x = infinity)
neg = limit(exp(atan(x)), x = -infinity)
However, it gives me an error saying "invalid syntax at =. Possibly, a ), }, or ] is missing. When I looked at the Matlab documentation for how to compute a limit, they had this as their example:
limit((1 + 1/n)^n, n = infinity)
and this returned an answer of e. When I put this into my own Matlab, it gave me the same error, could anyone help? Is it possibly an error with my Matlab?
You've been looking at the wrong help. Such notation (and limit function) is used in MuPAD interface, not in the simple Matlab Command Window.
To use limit() in Matlab environment, you have to use symbolic variables and this is the correct help page.
In other words, to compute
limit((1 + 1/n)^n, n = infinity)
you have to declare a symbolic variable n
syms n
and then provide the correct syntax (ref. help)
limit((1 + 1/n)^n, n, inf)
and the result is (of course) exp(1), that is e.
I did a number of symbolic manipulations and obtain an expression for my variable z. I used the following code to generate the FORTRAN code for z:
fortran(z,'file','FTRN_2Mkt_dfpa1');
The program stops after about 10 min, and I get these error messages:
??? Error using ==> mupadmex
Error in MuPAD command: Recursive definition [See ?MAXDEPTH]; during evaluation of 'generate::CFformatting'
Error in ==> sym.sym>sym.generateCode at 2169 tk = mupadmex(['generate::' lang], expr, 0);
Error in ==> sym.fortran at 43 generateCode(sym(t),'fortran',opts);
I think the problem is that the z expression is too long. The MuPAD software is treating this long expression as an infinite recursive operation. I am guessing that the MAXDEPTH in the fortran() source file is set at a level that is smaller than needed to convert the z expression to FORTRAN. If my guess is correct, is there a way to change MAXDEPTH in the fortran() source code?
If my guess is wrong, what can I do to generate the FORTRAN code for the z expression?
I really need the FORTRAN code for the symbolic expression of z. If you can help me, it would be wonderful. Thanks a million in advance!
Best, Limin
I've just updated to Matlab 2014a finally. I have loads of scripts that use the Symbolic Math Toolbox that used to work fine, but now hit the following error:
Error using mupadmex
Error in MuPAD command: Division by zero. [_power]
Evaluating: symobj::trysubs
I can't post my actual code here, but here is a simplified example:
syms f x y
f = x/y
results = double(subs(f, {'x','y'}, {1:10,-4:5}))
In my actual script I'm passing two 23x23 grids of values to a complicated function and I don't know in advance which of these values will result in the divide by zero. Everything I can find on Google just tells me not to attempt an evaluation that will result in the divide by zero. Not helpful! I used to get 'inf' (or 'NaN' - I can't specifically remember) for those it could not evaluate that I could easily filter for when I do the next steps on this data.
Does anyone know how to force Matlab 2014a back to that behaviour rather than throwing the error? Or am I doomed to running an older version of Matlab forever or going through the significant pain of changing my approach to this to avoid the divide by zero?
You could define a division which has the behaviour you want, this division function returns inf for division by zero:
mydiv=#(x,y)x/(dirac(y)+y)+dirac(y)
f = mydiv(x,y)
results = double(subs(f, {'x','y'}, {1:10,-4:5}))
I'm new to Matlab and I'm attempting to use it to solve equations numerically. I consulted the Matlab documentation, found the following code:
numeric::solve(x^6 - PI*x^2 = sin(3), x)
I tried to execute it, but Matlab says:
numeric::solve(x^6 - PI*x^2 = sin(3), x)
|
Error: Unexpected MATLAB operator.
I'm confused. Could you tell me what's wrong? I'm using Matlab R2013a on OS X Mavericks.
try using
syms x
sol_x = solve(x^6 - pi*x^2 == sin(3), x);
sol_x = sym2poly(sol_x);
You try to use a command that only works in the MuPAD Notebook Interface in MATLAB. The documentation got better over time pointing this out. Does 2013a include the vpasolve command? If yes, that is probably what you are looking for.
Perhaps you typed "solve" and "matlab" into Google and came across this page. Note the warning in the yellow box at the top of the page that #ChristopherCreutzig alluded to in his answer. MuPAD is a separate environment available to Matlab users – type mupad in your command window and you'll be able to run your command – but its functions are not directly callable from within Matlab. Many (if not most) of the functions in the Symbolic Math toolbox use the MuPAD engine under the hood. In your case you can call numeric::solve from within Matlab like this:
syms x;
s = feval(symengine, 'numeric::solve', x^6 - sym(pi)*x^2 == sin(3), x)
or using the older string format:
s = feval(symengine, 'numeric::solve', 'x^6 - pi*x^2 = sin(3)', 'x')
The output of either can then be converted to double precision column vector with s = double(s.'). However, in this case there seems to be no reason not to use sym/solve instead:
syms x
s = double(solve(x^6 - sym(pi)*x^2 == sin(3), x).');
See this for further details and other options for calling MuPAD functions from within Matlab.