how to write this in matlab. c=x+y; where x=p^2*e^j*p and y=q*e^(-j*q); p,q ranges from 1:1:k.
MATLAB: How to add two loops
loops
Related Solutions
Hari,
It seems you have answered your own question here, but just to clarify on the issue of accuracy, I will add a little to it.
Functions subs(s) vs matlabFunction(s) Accuracy
Using the functions|double(subs(s))| will definitely give you a more accurate evaluation of the symbolic expression, s, than matlabFunction(s) will.
This is because matlabFunction(s) breaks the symbolic expression s up into pieces (just look at your FileNmForDCritn for proof of this) which are evaluated and stored into double precision variables. Then the complete expression is evaluated (summed,multiplied,etc..) using these stored values. Each evaluation and storage is a chance for floating point error to occur and accumulate with subsequent steps.
The double(subs(s)) method of evaluating s, however, does not convert to double precision until the final step. Each intermediary step is computed with a "working" precision specified by digits (just like the function vpa). The default value for the the working precision is 32.
This means that every exp calculated in your symbolic expression is calculated to 32 digits of accuracy, roughly twice as much as when using a double.
Functions matlabFunction(s1) vs matlabFunction(s1,s2) Accuracy
This difference is harder to predict and compare, since it does not seem like there should be any reason these two methods of evaluating s1 arrive at different values. When you look inside the two function created (like your FileNmForDCritn and FileNmForDCritnObj_Grad), however, you will probably see vastly different expressions for s1 in each file. Before breaking the expressions up, matlabFunction first tries to simplify and optimize the pieces it stores as doubles taking into consideration both outputs the function will be producing simultaneously, not one at a time. This means that the order of operations can be different for each function.
Again, what it ultimately boils down to is that floating point arithmetic does not necessarily satisfy associative, distributive or commutative laws, so how you evaluate an expression matters.
a1 = (-44*Pi^4-1875*P1)*R/(9600*Pi^4*R-1100*Pi^4-75*P3)a2 = (-1188*Pi^4-625*P2)*R/(19200*Pi^4*R-3300*Pi^4-225*P3)a3 = R
where
R = roots([2899102924800000000000*Pi^20, -113246208000000000000*Pi^16*((44/3)*Pi^4+P3), 4423680000000000000*((108416/146484375)*Pi^10+(243960584/3515625)*Pi^8+((88/1875)*P1+(594/625)*P2+(814/75)*P3)*Pi^4+P1^2+(1/4)*P2^2+(37/100)*P3^2)*Pi^12, -103680000000000000*((44/3)*Pi^4+P3)*((16153984/10986328125)*Pi^10+(35264208/9765625)*Pi^8+((88/1875)*P1+(396/625)*P2+(44/15)*P3)*Pi^4+P1^2+(1/6)*P2^2+(1/10)*P3^2)*Pi^8, 607500000000000*((44/3)*Pi^4+P3)^2*Pi^4*((30464896/6591796875)*Pi^10-(1931850032/52734375)*Pi^8+((88/1875)*P1+(264/625)*P2+(88/75)*P3)*Pi^4+P1^2+(1/9)*P2^2+(1/25)*P3^2), -86016*(11011*Pi^2-75623375)*Pi^8*(44*Pi^4+3*P3)^3, 11760*Pi^4*(44*Pi^4+3*P3)^4*(121*Pi^2-432125), -(847*Pi^2+125)*(44*Pi^4+3*P3)^5])
... All of the roots. The polynomial is degree 7, so there will be 7 roots, and so 7 solutions to the overall equations.
Best Answer