how to convert contour plot from mathematica to matlab - matlab

I am trying to draw a contour for an equation which I got from Mathematica. I was able to draw this contour easily in mathematica but not on matlab. Kindly suggest.
b=linspace(0.0005,0.0010,100);
c=linspace(0.64,0.7,100);
z=(-136*power(2.718281828459045,power(88682,c)*b)*(1 - 1/power(2.718281828459045,1*power(88682,c)*b)))/(-1+ power(2.718281828459045,power(88682,c)*b)) - 1*(power(3,c) + power(33,c) + power(146,c) + power(227,c) + power(342,c) + power(351,c) + power(353,c) + power(444,c) + power(556,c) + power(571,c) + power(709,c) + power(759,c) + power(836,c) + power(860,c) + power(968,c) + power(1056,c) + power(1726,c) + power(1846,c) + power(1872,c) + power(1986,c) + power(2311,c) + power(2366,c) + power(2608,c) + power(2676,c) + power(3098,c) + power(3278,c) + power(3288,c) + power(4434,c) + power(5034,c) + power(5049,c) + power(5085,c) + 2*power(5089,c) + power(5097,c) + power(2,1. + 3.*c)*power(5287,c) + power(5324,c) + power(5389,c) + power(5565,c) + power(5623,c) + power(6080,c) + power(6380,c) + power(6477,c) + power(6740,c) + power(7192,c) + power(7447,c) + power(7644,c) + power(7837,c) + power(7843,c) + power(7922,c) + power(8738,c) + power(10089,c) + power(10237,c) + power(10258,c) + power(10491,c) + power(10625,c) + power(10982,c) + power(11175,c) + power(11411,c) + power(11442,c) + power(11811,c) + 2.*power(12559,c) + power(12791,c) + power(13121,c) + power(13486,c) + power(14708,c) + power(15251,c) + power(15261,c) + power(15277,c) + power(15806,c) + power(16185,c) + power(16229,c) + power(16358,c) + power(17168,c) + power(17458,c) + power(17758,c) + power(18287,c) + power(18568,c) + power(18728,c) + power(19556,c) + power(20567,c) + power(21012,c) + power(21308,c) + power(23063,c) + power(24127,c) + power(25910,c) + power(26770,c) + power(27753,c) + power(28460,c) + power(28493,c) + power(29361,c) + power(30085,c) + power(32408,c) + power(35338,c) + power(36799,c) + power(37642,c) + power(37654,c) + power(37915,c) + power(39715,c) + power(40580,c) + power(42015,c) + power(42045,c) + power(42188,c) + power(45406,c) + power(46653,c) + power(47596,c) + power(48296,c) + power(49171,c) + power(49416,c) + power(50145,c) + power(52042,c) + power(52489,c) + power(52875,c) + power(53321,c) + power(53443,c) + power(54433,c) + power(55381,c) + power(56463,c) + power(56485,c) + power(56560,c) + power(57042,c) + power(62551,c) + power(62651,c) + power(62661,c) + power(63732,c) + power(64103,c) + power(64893,c) + power(71043,c) + power(74364,c) + power(75409,c) + power(76057,c) + power(81542,c) + power(82702,c) + power(84566,c) + power(88682,c))*b + 1266.5546418385234*(-1 + c) + 136.*log((136.*power(2.718281828459045,power(88682,c)*b))/(-1 + power(2.718281828459045,power(88682,c)*b))) + 136*log(b) + 136*log(c);
contour(b,c,z)

The thing to keep in mind is that Mathematica is formula-oriented and is programmed in terms of mathematical variables, while Matlab is numerically oriented and programmed in terms of matrices and arrays. To evaluate a function of two variables on a grid, it is necessary to first generate that grid explicitly in the form of arrays, and then to take care to use the element-wise operators (.*, ./ etc.) instead of the matrix operators. Here is the code:
[B, C] = meshgrid(b, c);
z = (-136*exp(power(88682,C) .* B) .* (1 - 1./exp(1*power(88682,C) .* B)))./(-1+ exp(power(88682,C) .* B)) ...
- 1*(power(3,C) + power(33,C) + power(146,C) + power(227,C) + power(342,C) + power(351,C) + power(353,C) + power(444,C) + power(556,C) + power(571,C) + power(709,C) + power(759,C) + power(836,C) + power(860,C) + power(968,C) + power(1056,C) + power(1726,C) + power(1846,C) + power(1872,C) + power(1986,C) + power(2311,C) + power(2366,C) + power(2608,C) + power(2676,C) + power(3098,C) + power(3278,C) + power(3288,C) + power(4434,C) + power(5034,C) + power(5049,C) + power(5085,C) + 2*power(5089,C) + power(5097,C) + power(2,1 + 3*C) .* power(5287,C) + power(5324,C) + power(5389,C) + power(5565,C) + power(5623,C) + power(6080,C) + power(6380,C) + power(6477,C) + power(6740,C) + power(7192,C) + power(7447,C) + power(7644,C) + power(7837,C) + power(7843,C) + power(7922,C) + power(8738,C) + power(10089,C) + power(10237,C) + power(10258,C) + power(10491,C) + power(10625,C) + power(10982,C) + power(11175,C) + power(11411,C) + power(11442,C) + power(11811,C) + 2*power(12559,C) + power(12791,C) + power(13121,C) + power(13486,C) + power(14708,C) + power(15251,C) + power(15261,C) + power(15277,C) + power(15806,C) + power(16185,C) + power(16229,C) + power(16358,C) + power(17168,C) + power(17458,C) + power(17758,C) + power(18287,C) + power(18568,C) + power(18728,C) + power(19556,C) + power(20567,C) + power(21012,C) + power(21308,C) + power(23063,C) + power(24127,C) + power(25910,C) + power(26770,C) + power(27753,C) + power(28460,C) + power(28493,C) + power(29361,C) + power(30085,C) + power(32408,C) + power(35338,C) + power(36799,C) + power(37642,C) + power(37654,C) + power(37915,C) + power(39715,C) + power(40580,C) + power(42015,C) + power(42045,C) + power(42188,C) + power(45406,C) + power(46653,C) + power(47596,C) + power(48296,C) + power(49171,C) + power(49416,C) + power(50145,C) + power(52042,C) + power(52489,C) + power(52875,C) + power(53321,C) + power(53443,C) + power(54433,C) + power(55381,C) + power(56463,C) + power(56485,C) + power(56560,C) + power(57042,C) + power(62551,C) + power(62651,C) + power(62661,C) + power(63732,C) + power(64103,C) + power(64893,C) + power(71043,C) + power(74364,C) + power(75409,C) + power(76057,C) + power(81542,C) + power(82702,C) + power(84566,C) + power(88682,C)) .* B ...
+ 1266.5546418385234*(-1 + C) + 136*log((136*exp(power(88682,C) .* B))./(-1 + exp(power(88682,C) .* B))) + 136*log(B) + 136*log(C);
contour(b, c, z)
I also took the liberty to slightly syntax-normalize and simplify your formula (e.g, power(2.718..., x) is exp(x)).
Here's the result:
If you want more contour lines, use something like contour(b, c, z, 100).
To understand what I've done here, read Array vs. Matrix operations and meshgrid from the Matlab documentation. Because .* means something else than * in Matlab, one should also take care not to have trailing decimal points in expressions, like 3.*B, though it doesn't really do any harm in that case.

Related

MATLAB: When specifying the color of points, legend does not match

I am using MATLAB R2017b.
This section of the code is generating the data to be plotted:
% Parameters
cE = 0.1;
cs = 0.1;
a0 = 0.5;
a = 0.5;
L= 0.1;
b=1
% Parameter in inspection
eStart=0.0; %inicial value of parameter
eStep=0.01; %how far apart is each value
eEnd=1; %Final value of parameter
e = eStart:eStep:eEnd; %Array containing all parameters
nPoints = length(e); %number of parameter values used
T=700; % time interval
transientCut = 500; %indicates the cut to avoid seeing transient
% points (points before reach equilibtium);
%thus as transientCut increases the graph will start to
%show points that did not reach equilibrium yet
%setting the matrix that will save the time series for each value of parameter used
XIR=zeros(nPoints,T);
XiR=zeros(nPoints,T);
XIr=zeros(nPoints,T);
Xir=zeros(nPoints,T);
for k=1:nPoints %outer loop gives the condition in which the eValues
% will be accesed
xiR =0.5 %IC
xIr =0.20 %IC
xir = 0.3 %IC
xIR= 1-xiR-xIr-xir;
for t=1:T
%Difference equation
xIR=(xIR*(1 + (a0 + a*(L*xIr + xIR))*(-cE - cs + (1 - xir - xIr + (xir + xIr)*e(k))^b)))/(xiR + xIr + xIR - cE*xiR*(a0 + a*(L*xIr + xIR)) - (cE + cs)*(a0*(xIr + xIR) + a*xIR*(L*xIr + xIR)) + xir*(1 - a0*cE + (a0 + a*(L*xIr + xIR))*((-1 + xir + xIr)*(-1 + e(k)))^b) + xIr*(a0 + a*(L*xIr + xIR))*((-1 + xir + xIr)*(-1 + e(k)))^b + a0*(xir + xIr)*e(k)^b + (xiR + xIR)*(a0 + a*(L*xIr + xIR))*(1 - xir - xIr + (xir + xIr)*e(k))^b);
xiR=(xiR + xiR*(a0 + a*(L*xIr + xIR))*(-cE + (1 - xir - xIr + (xir + xIr)*e(k))^b))/(xiR + xIr + xIR - cE*xiR*(a0 + a*(L*xIr + xIR)) - (cE + cs)*(a0*(xIr + xIR) + a*xIR*(L*xIr + xIR)) + xir*(1 - a0*cE + (a0 + a*(L*xIr + xIR))*((-1 + xir + xIr)*(-1 + e(k)))^b) + xIr*(a0 + a*(L*xIr + xIR))*((-1 + xir + xIr)*(-1 + e(k)))^b + a0*(xir + xIr)*e(k)^b + (xiR + xIR)*(a0 + a*(L*xIr + xIR))*(1 - xir - xIr + (xir + xIr)*e(k))^b);
xIr=(xIr*(1 - a0*(cE + cs) + (a0 + a*(L*xIr + xIR))*((-1 + xir + xIr)*(-1 + e(k)))^b + a0*e(k)^b))/(xiR + xIr + xIR - cE*xiR*(a0 + a*(L*xIr + xIR)) - (cE + cs)*(a0*(xIr + xIR) + a*xIR*(L*xIr + xIR)) + xir*(1 - a0*cE + (a0 + a*(L*xIr + xIR))*((-1 + xir + xIr)*(-1 + e(k)))^b) + xIr*(a0 + a*(L*xIr + xIR))*((-1 + xir + xIr)*(-1 + e(k)))^b + a0*(xir + xIr)*e(k)^b + (xiR + xIR)*(a0 + a*(L*xIr + xIR))*(1 - xir - xIr + (xir + xIr)*e(k))^b);
xir=(xir*(1 - a0*cE + (a0 + a*(L*xIr + xIR))*((-1 + xir + xIr)*(-1 + e(k)))^b + a0*e(k)^b))/(xiR + xIr + xIR - cE*xiR*(a0 + a*(L*xIr + xIR)) - (cE + cs)*(a0*(xIr + xIR) + a*xIR*(L*xIr + xIR)) + xir*(1 - a0*cE + (a0 + a*(L*xIr + xIR))*((-1 + xir + xIr)*(-1 + e(k)))^b) + xIr*(a0 + a*(L*xIr + xIR))*((-1 + xir + xIr)*(-1 + e(k)))^b + a0*(xir + xIr)*e(k)^b + (xiR + xIR)*(a0 + a*(L*xIr + xIR))*(1 - xir - xIr + (xir + xIr)*e(k))^b);
%saving for each value of e (at row k), the time serie
XIR(k,t)=xIR;
XiR(k,t)=xiR;
XIr(k,t)=xIr;
Xir(k,t)=xir;
end
end
Here is where the problem is.
If I let the MATLAB default choose the colors the legend matches the color.
But if I specify the shape and color of the data, then legend does not match these specifications (see attached below)
figure
plot(eStart:eStep:eEnd, XIR(:,transientCut:T),'g*' ,eStart:eStep:eEnd, XiR(:,transientCut:T),'b.' ...
,eStart:eStep:eEnd, XIr(:,transientCut:T),'r.' ,eStart:eStep:eEnd, Xir(:,transientCut:T),'k.')
xlabel('xlable','FontSize',12)
ylabel('ylable','FontSize',12)
ylim([0 1])
title(['c_{s}=',num2str(cs),', c_{E}=',num2str(cE),...
', L=',num2str(L),', a_{0}=',num2str(a0), ', a=',num2str(a)])
legend('XIR','XiR','XIr','Xir')
Notice the legend does not match the color and shape in the graph
I also tried suggestions of similar issues that I found in this forum, but the legend still does not match the color/shape specifications:
p1=plot(eStart:eStep:eEnd, XIR(:,transientCut:T),'g*');
hold on
p2=plot(eStart:eStep:eEnd, XiR(:,transientCut:T),'b.');
p3=plot(eStart:eStep:eEnd, Xir(:,transientCut:T),'r.');
p4=plot(eStart:eStep:eEnd, Xir(:,transientCut:T),'k.');
xlabel('xlable','FontSize',12)
ylabel('ylable','FontSize',12)
ylim([0 1])
title(['c_{s}=',num2str(cs),', c_{E}=',num2str(cE),...
', L=',num2str(L),', a_{0}=',num2str(a0), ', a=',num2str(a)])
legend([p1;p2;p3;p4], {'xIR','xiR','xIr','xir'})
ylabel('ylable','FontSize',12)
ylim([0 1])
title(['c_{s}=',num2str(cs),', c_{E}=',num2str(cE),...
', L=',num2str(L),', a_{0}=',num2str(a0), ', a=',num2str(a)])
legend('XIR','XiR','XIr','Xir')
The problem is the shape of your data. Your data for the y-axis, XIR, Xir, ... are all matrices.
So when you use the command: plot(eStart:eStep:eEnd, XIR(:,transientCut:T),'g*'), you are getting more than just one data series.
Try this out and you will see what I mean:
plot(eStart:eStep:eEnd, XIR(:,transientCut:T),'g*')
legend
Where you were supposedly trying to plot one data series XIR, there will be 201 series instead. (size of eStart:eStep:eEnd is a [1,101] vector, while size of XIR(:,transientCut:T) is a [101,201] matrix).
To fix this, you have two options.
Sort out how you want to plot your data. So convert the datasets like XIR(:,transientCut:T) into a vector when plotting.
If you really need to plot it with that code you have, you can "trick" matlab, by plotting an empty dataset with the same markers before your dataset.
figure
hold on
plot(NaN, NaN, 'g*') % Same marker style as XIR
plot(NaN, NaN, 'b.') % Same marker style as XiR
plot(NaN, NaN, 'r.') % Same marker style as XIr
plot(NaN, NaN, 'k.') % Same marker style as Xir
% Plot your data as normal
plot(eStart:eStep:eEnd, XIR(:,transientCut:T),'g*' ,eStart:eStep:eEnd, XiR(:,transientCut:T),'b.' ...
,eStart:eStep:eEnd, XIr(:,transientCut:T),'r.' ,eStart:eStep:eEnd, Xir(:,transientCut:T),'k.')
% Call legend after plotting your data
legend('XIR','XiR','XIr','Xir')
P.S. I recommend that you rename your variables too. XIR, XiR, XIr, Xir are all too easy to switch up, as can be seen in your last code snippet:
p1=plot(eStart:eStep:eEnd, XIR(:,transientCut:T),'g*');
hold on
p2=plot(eStart:eStep:eEnd, XiR(:,transientCut:T),'b.');
p3=plot(eStart:eStep:eEnd, Xir(:,transientCut:T),'r.'); <-- XIr, not Xir?
p4=plot(eStart:eStep:eEnd, Xir(:,transientCut:T),'k.');

Matlab: Nonlinear Eq: error

I have the code from a previous question, however, this one is similar, just more equations added. However, I get an error and I'm not sure how to fix it.
Link to my previous question: Matlab: Nonlinear equation solver
function F = fcn(x)
F=[x(6) + x(7) + x(8) + x(9) + x(10) - 2 ;
x(6)*x(1) + x(7)*x(2) + x(8)*x(3) + x(9)*x(4) + x(10)*x(5) ;
x(6)*x(1)^2 + x(7)*x(2)^2 + x(8)*x(3)^2 + x(9)*x(4)^2 + x(10)*x(5) - 2/3 ;
x(6)*x(1)^3 + x(7)*x(2)^3 + x(8)*x(3)^3 + x(9)*x(4)^3 + x(10)*x(5) ;
x(6)*x(1)^4 + x(7)*x(2)^4 + x(8)*x(3)^4 + x(9)*x(4)^4 + x(10)*x(5) -2/5 ;
x(6)*x(1)^5 + x(7)*x(2)^5 + x(8)*x(3)^5 + x(9)*x(4)^5 + x(10)*x(5) ;
x(6)*x(1)^6 + x(7)*x(2)^6 + x(8)*x(3)^6 + x(9)*x(4)^6 + x(10)*x(5) -2/7 ;
x(6)*x(1)^7 + x(7)*x(2)^7 + x(8)*x(3)^7 + x(9)*x(4)^7 + x(10)*x(5) ;
x(6)*x(1)^8 + x(7)*x(2)^8 + x(8)*x(3)^8 + x(9)*x(4)^8 + x(10)*x(5) -2/9 ;
x(6)*x(1)^9 + x(7)*x(2)^9 + x(8)*x(3)^9 + x(9)*x(4)^9 + x(10)*x(5)
];
end
clc
clear all;
format long
x0 = [0.9; 0.5; 0.1; -0.5; -0.9; 0.2; 0.4; 0.5; 0.4; 0.2]; %Guess
F0 = fcn(x0);
[x,fval]=fsolve(#fcn, x0) %solve without optimization
options = optimset('MaxFunEvals',10000, 'MaxIter', 10000); %optimization criteria
[x,fval]=fsolve(#fcn, x0, options) %solve with optimization
The error that I get are:
Error using vertcat
Dimensions of matrices being concatenated are not consistent.
Error in fcn(line 4) %This is from the function script
F=[x(6) + x(7) + x(8) + x(9) + x(10)
- 2 ;
Error in fcncall (line 7) %This is from the main script
F0 = fcn(x0);
This is a very subtle error I've encountered before. When creating an array literal as you are doing fcn, negative signs that are directly attached to numbers and preceded by a space, as in rows 5, 7, and 9 of your array literal, are viewed as unary operators (i.e., the - makes the number negative and does not act as a binary minus operation). Therefore, because Matlab allows the delimiting of columns to be made with spaces, the indicated rows are interpreted to have two columns; row 5 column 1 is x(6)*x(1)^4 ... x(10)*x(5), and row 5 column 2 is -2/5.
So, either put a space between the three numbers or eliminate all spaces between the minus signs. For example:
x(6)*x(1)^4 + x(7)*x(2)^4 + x(8)*x(3)^4 + x(9)*x(4)^4 + x(10)*x(5) - 2/5;
or
x(6)*x(1)^4 + x(7)*x(2)^4 + x(8)*x(3)^4 + x(9)*x(4)^4 + x(10)*x(5)-2/5;
That problem wasn't easy to find. Not using a blank before the minus at the end of the rows, you created a row with two elements instead of one. For a simplified example compare this:
>> [2 - 2]
ans =
0
>> [2 -2]
ans =
2 -2
>> [(2 -2)]
ans =
0
Now the corrected version of your code:
F=[x(6) + x(7) + x(8) + x(9) + x(10) - 2 ;
x(6)*x(1) + x(7)*x(2) + x(8)*x(3) + x(9)*x(4) + x(10)*x(5) ;
x(6)*x(1)^2 + x(7)*x(2)^2 + x(8)*x(3)^2 + x(9)*x(4)^2 + x(10)*x(5) - 2/3 ; ...either set a space
x(6)*x(1)^3 + x(7)*x(2)^3 + x(8)*x(3)^3 + x(9)*x(4)^3 + x(10)*x(5) ;
(x(6)*x(1)^4 + x(7)*x(2)^4 + x(8)*x(3)^4 + x(9)*x(4)^4 + x(10)*x(5) -2/5); ...or use brackets
x(6)*x(1)^5 + x(7)*x(2)^5 + x(8)*x(3)^5 + x(9)*x(4)^5 + x(10)*x(5) ; ...both fixes the problem
x(6)*x(1)^6 + x(7)*x(2)^6 + x(8)*x(3)^6 + x(9)*x(4)^6 + x(10)*x(5) - 2/7 ;
x(6)*x(1)^7 + x(7)*x(2)^7 + x(8)*x(3)^7 + x(9)*x(4)^7 + x(10)*x(5) ;
x(6)*x(1)^8 + x(7)*x(2)^8 + x(8)*x(3)^8 + x(9)*x(4)^8 + x(10)*x(5) - 2/9 ;
x(6)*x(1)^9 + x(7)*x(2)^9 + x(8)*x(3)^9 + x(9)*x(4)^9 + x(10)*x(5) ];

The fsolve matlab function not converging

I have a system of 6 equations in 6 unknowns and I am trying to solve it using fsolve. A strange thing arises though. When I keep 6 in eq's in 6 un's as they are and call fsolve they solver does not converge and is very slow. But when I reduce equations in 5 with 6 unknowns (I can do this by substituting one of my equations in the rest) and by using the 'levenberg-marquardt' the solver converges really fast and reaches a solution. While the solutions I get are good I am not sure if they are correct and I cannot understand why this happens. I also tried to set as initial values in the 6x6 case the solution I got from the 5x6 case but again the fsolve is not converging. I don't know if it is a mathematical problem or fsolve's problem
Any thoughts?
Thank you
p.s I am not posting the code, it is really big, My system is first written in symbolic and then i substitute symbolic variables with numeric so as to solve it with fsolve.
function f=examplegeneral(x);
beta11=0.5; beta12=0.5; beta21=0.5; beta22=0.5; b11=0.5; b12=0.5; b21=0.5; b22=0.5; v1=0.5; v2=0.5;
f(1)=x(2)*(1/beta11)^beta11*((x(1) + x(4) + x(6))/beta12)^beta12 - v1*(b12*x(1)^2 + b11)^(1/2) - x(2)*(x(4) + x(6))*(1/beta11)^beta11*((x(1) + x(4) + x(6))/beta12)^(beta12 - 1);
f(2)=x(3)*(1/beta21)^beta21*((x(1) + x(5) + x(6))/beta22)^beta22 - v2*(b22*x(1)^2 + b21)^(1/2) - x(3)*(x(5) + x(6))*(1/beta21)^beta21*((x(1) + x(5) + x(6))/beta22)^(beta22 - 1);
f(3)=(x(2)*(x(4) + x(6))*(beta12 - 1)*(1/beta11)^beta11*((x(1) + x(4) + x(6))/beta12)^(beta12 - 2))/beta12 - (x(2)*(beta12 - 1)*(1/beta11)^beta11*((x(1) + x(4) + x(6))/beta12)^(beta12 - 2)*((b12*x(1)*v1)/(b12*x(1)^2 + b11)^(1/2) - x(2)*(1/beta11)^beta11*((x(1) + x(4) + x(6))/beta12)^(beta12 - 1) + (x(2)*(x(4) + x(6))*(beta12 - 1)*(1/beta11)^beta11*((x(1) + x(4) + x(6))/beta12)^(beta12 - 2))/beta12))/(beta12*((b12^2*x(1)^2*v1)/(b12*x(1)^2 + b11)^(3/2) - (b22*v2)/(b22*x(1)^2 + b21)^(1/2) - (b12*v1)/(b12*x(1)^2 + b11)^(1/2) + (b22^2*x(1)^2*v2)/(b22*x(1)^2 + b21)^(3/2) + (x(2)*(beta12 - 1)*(1/beta11)^beta11*((x(1) + x(4) + x(6))/beta12)^(beta12 - 2))/beta12 + (x(3)*(beta22 - 1)*(1/beta21)^beta21*((x(1) + x(5) + x(6))/beta22)^(beta22 - 2))/beta22));
f(4)=(x(3)*(x(5) + x(6))*(beta22 - 1)*(1/beta21)^beta21*((x(1) + x(5) + x(6))/beta22)^(beta22 - 2))/beta22 - (x(3)*(beta22 - 1)*(1/beta21)^beta21*((x(1) + x(5) + x(6))/beta22)^(beta22 - 2)*((b22*x(1)*v2)/(b22*x(1)^2 + b21)^(1/2) - x(3)*(1/beta21)^beta21*((x(1) + x(5) + x(6))/beta22)^(beta22 - 1) + (x(3)*(x(5) + x(6))*(beta22 - 1)*(1/beta21)^beta21*((x(1) + x(5) + x(6))/beta22)^(beta22 - 2))/beta22))/(beta22*((b12^2*x(1)^2*v1)/(b12*x(1)^2 + b11)^(3/2) - (b22*v2)/(b22*x(1)^2 + b21)^(1/2) - (b12*v1)/(b12*x(1)^2 + b11)^(1/2) + (b22^2*x(1)^2*v2)/(b22*x(1)^2 + b21)^(3/2) + (x(2)*(beta12 - 1)*(1/beta11)^beta11*((x(1) + x(4) + x(6))/beta12)^(beta12 - 2))/beta12 + (x(3)*(beta22 - 1)*(1/beta21)^beta21*((x(1) + x(5) + x(6))/beta22)^(beta22 - 2))/beta22));
f(5)=(x(2)*(x(4) + x(6))*(beta12 - 1)*(1/beta11)^beta11*((x(1) + x(4) + x(6))/beta12)^(beta12 - 2))/beta12 - (((x(2)*(beta12 - 1)*(1/beta11)^beta11*((x(1) + x(4) + x(6))/beta12)^(beta12 - 2))/beta12 + (x(3)*(beta22 - 1)*(1/beta21)^beta21*((x(1) + x(5) + x(6))/beta22)^(beta22 - 2))/beta22)*((x(2)*(x(4) + x(6))*(beta12 - 1)*(1/beta11)^beta11*((x(1) + x(4) + x(6))/beta12)^(beta12 - 2))/beta12 + (x(3)*(x(5) + x(6))*(beta22 - 1)*(1/beta21)^beta21*((x(1) + x(5) + x(6))/beta22)^(beta22 - 2))/beta22))/((b12^2*x(1)^2*v1)/(b12*x(1)^2 + b11)^(3/2) - (b22*v2)/(b22*x(1)^2 + b21)^(1/2) - (b12*v1)/(b12*x(1)^2 + b11)^(1/2) + (b22^2*x(1)^2*v2)/(b22*x(1)^2 + b21)^(3/2) + (x(2)*(beta12 - 1)*(1/beta11)^beta11*((x(1) + x(4) + x(6))/beta12)^(beta12 - 2))/beta12 + (x(3)*(beta22 - 1)*(1/beta21)^beta21*((x(1) + x(5) + x(6))/beta22)^(beta22 - 2))/beta22) + (x(3)*(x(5) + x(6))*(beta22 - 1)*(1/beta21)^beta21*((x(1) + x(5) + x(6))/beta22)^(beta22 - 2))/beta22;
f(6)=1/((b12^2*x(1)^2*v1)/(b12*x(1)^2 + b11)^(3/2) - (b22*v2)/(b22*x(1)^2 + b21)^(1/2) - (b12*v1)/(b12*x(1)^2 + b11)^(1/2) + (b22^2*x(1)^2*v2)/(b22*x(1)^2 + b21)^(3/2) + (x(2)*(beta12 - 1)*(1/beta11)^beta11*((x(1) + x(4) + x(6))/beta12)^(beta12 - 2))/beta12 + (x(3)*(beta22 - 1)*(1/beta21)^beta21*((x(1) + x(5) + x(6))/beta22)^(beta22 - 2))/beta22);
and then i call
x0=[0.8,0.05,0.05,0.01,0.01,-0.01]; % This is the vector of initial values.
options = optimset('Display','iter','MaxFunEvals',1e+15,'MaxIter',1000000000);
[sol,exitflag,fval]=fsolve('examplegeneral',x0,options)

Evaluate solution from Laplace

The following variables were the solutions of a system of ODE by means of Laplace transform. The question is, how can I evaluate this as a function of t?
zs =
(426453367703541750000*sin(t))/422998807223231216101 - (126885219268500000*cos(t))/422998807223231216101 - (143127622539985885236418500000*sum(exp(r3*t)/(2*(169386*r3^3 + 3312090*r3^2 + 336237395*r3 + 891000000)), r3 in RootOf(s3^4 + (736020*s3^3)/28231 + (336237395*s3^2)/84693 + (594000000*s3)/28231 + 13810500000/28231, s3)))/422998807223231216101 - (898981813790513658018000000*sum((r3*exp(r3*t))/(2*(169386*r3^3 + 3312090*r3^2 + 336237395*r3 + 891000000)), r3 in RootOf(s3^4 + (736020*s3^3)/28231 + (336237395*s3^2)/84693 + (594000000*s3)/28231 + 13810500000/28231, s3)))/422998807223231216101 - (35837444893658057322750000*sum((r3^2*exp(r3*t))/(2*(169386*r3^3 + 3312090*r3^2 + 336237395*r3 + 891000000)), r3 in RootOf(s3^4 + (736020*s3^3)/28231 + (336237395*s3^2)/84693 + (594000000*s3)/28231 + 13810500000/28231, s3)))/422998807223231216101 + (10746289875507070500000*sum((r3^3*exp(r3*t))/(2*(169386*r3^3 + 3312090*r3^2 + 336237395*r3 + 891000000)), r3 in RootOf(s3^4 + (736020*s3^3)/28231 + (336237395*s3^2)/84693 + (594000000*s3)/28231 + 13810500000/28231, s3)))/422998807223231216101
zu =
(423526351592241700000*sin(t))/422998807223231216101 - (119538342000000*cos(t))/422998807223231216101 - (21856954524657863661418500000*sum(exp(r3*t)/(2*(169386*r3^3 + 3312090*r3^2 + 336237395*r3 + 891000000)), r3 in RootOf(s3^4 + (736020*s3^3)/28231 + (336237395*s3^2)/84693 + (594000000*s3)/28231 + 13810500000/28231, s3)))/422998807223231216101 - (935131412760109308018000000*sum((r3*exp(r3*t))/(2*(169386*r3^3 + 3312090*r3^2 + 336237395*r3 + 891000000)), r3 in RootOf(s3^4 + (736020*s3^3)/28231 + (336237395*s3^2)/84693 + (594000000*s3)/28231 + 13810500000/28231, s3)))/422998807223231216101 - (35869453347570289778100000*sum((r3^2*exp(r3*t))/(2*(169386*r3^3 + 3312090*r3^2 + 336237395*r3 + 891000000)), r3 in RootOf(s3^4 + (736020*s3^3)/28231 + (336237395*s3^2)/84693 + (594000000*s3)/28231 + 13810500000/28231, s3)))/422998807223231216101 + (10124060799006000000*sum((r3^3*exp(r3*t))/(2*(169386*r3^3 + 3312090*r3^2 + 336237395*r3 + 891000000)), r3 in RootOf(s3^4 + (736020*s3^3)/28231 + (336237395*s3^2)/84693 + (594000000*s3)/28231 + 13810500000/28231, s3)))/422998807223231216101
Are these built with the Symbolic Math Toolbox?
Try
t=0:0.01:10;
values_s = zs(t);
values_u = zu(t);

sorting the roots from the solution of an equation

I have the following equation and I solve it using the Function "Solve" and the answer is as follows
syms t px py pz dx dy dz A21 A20 A19 A18 A17 A16 A15 A14 A13 A12 A11 A10 A9 A8 A7 A6 A5 A4 A3 A2 A1 Thickness;
Equation = A21.*((py +t*dy).^5) + (A20.*((py +t*dy).^4)).*(px +t*dx) + A19.*((py +t*dy).^4) + (A18.*((py +t*dy).^3)).*((px +t*dx).^2) + (A17.*((py +t*dy).^3)).*(px +t*dx) + A16.*((py +t*dy).^3) + (A15.*((py +t*dy).^2)).*((px +t*dx).^3) + (A14.*((py +t*dy).^2)).*((px +t*dx).^2) + (A13.*((py +t*dy).^2)).*(px +t*dx) + A12.*((py +t*dy).^2) + (A11.*((py +t*dy))).*((px +t*dx).^4) + (A10.*(py +t*dy)).*((px +t*dx).^3) + (A9.*(py +t*dy)).*((px +t*dx).^2) + (A8.*(py +t*dy)).*((px +t*dx)) + (A7.*(py +t*dy)) + A6.*((px +t*dx).^5) + A5.*((px +t*dx).^4) + A4.*((px +t*dx).^3) + A3.*((px +t*dx).^2) + A2.*(px +t*dx) + A1 - (pz +t*dz)
Answer = solve(S,t)
Answer = RootOf(A20*dx*dy^4*z^5 + A11*dx^4*dy*z^5 + A18*dx^2*dy^3*z^5 + A15*dx^3*dy^2*z^5 + A6*dx^5*z^5 + A21*dy^5*z^5 + 3*A18*dx^2*dy^2*py*z^4 + 3*A15*dx^2*dy^2*px*z^4 + 4*A20*dx*dy^3*py*z^4 + 2*A15*dx^3*dy*py*z^4 + 2*A18*dx*dy^3*px*z^4 + 4*A11*dx^3*dy*px*z^4 + 5*A6*dx^4*px*z^4 + 5*A21*dy^4*py*z^4 + A20*dy^4*px*z^4 + A11*dx^4*py*z^4 + A17*dx*dy^3*z^4 + A10*dx^3*dy*z^4 + A14*dx^2*dy^2*z^4 + A5*dx^4*z^4 + A19*dy^4*z^4 + 6*A18*dx*dy^2*px*py*z^3 + 6*A15*dx^2*dy*px*py*z^3 + 6*A20*dx*dy^2*py^2*z^3 + 3*A18*dx^2*dy*py^2*z^3 + 3*A15*dx*dy^2*px^2*z^3 + 6*A11*dx^2*dy*px^2*z^3 + 4*A20*dy^3*px*py*z^3 + 4*A11*dx^3*px*py*z^3 + 3*A17*dx*dy^2*py*z^3 + 2*A14*dx^2*dy*py*z^3 + 2*A14*dx*dy^2*px*z^3 + 3*A10*dx^2*dy*px*z^3 + 4*A5*dx^3*px*z^3 + 4*A19*dy^3*py*z^3 + 10*A6*dx^3*px^2*z^3 + 10*A21*dy^3*py^2*z^3 + A17*dy^3*px*z^3 + A10*dx^3*py*z^3 + A9*dx^2*dy*z^3 + A13*dx*dy^2*z^3 + A18*dy^3*px^2*z^3 + A15*dx^3*py^2*z^3 + A4*dx^3*z^3 + A16*dy^3*z^3 + 6*A18*dx*dy*px*py^2*z^2 + 6*A15*dx*dy*px^2*py*z^2 + 4*A14*dx*dy*px*py*z^2 + 2*A9*dx*dy*px*z^2 + 2*A13*dx*dy*py*z^2 + 6*A20*dy^2*px*py^2*z^2 + 3*A18*dy^2*px^2*py*z^2 + 3*A15*dx^2*px*py^2*z^2 + 6*A11*dx^2*px^2*py*z^2 + 3*A17*dy^2*px*py*z^2 + 3*A10*dx^2*px*py*z^2 + 4*A20*dx*dy*py^3*z^2 + 3*A17*dx*dy*py^2*z^2 + 4*A11*dx*dy*px^3*z^2 + 3*A10*dx*dy*px^2*z^2 + 3*A4*dx^2*px*z^2 + 3*A16*dy^2*py*z^2 + 10*A6*dx^2*px^3*z^2 + 6*A5*dx^2*px^2*z^2 + 10*A21*dy^2*py^3*z^2 + 6*A19*dy^2*py^2*z^2 + A9*dx^2*py*z^2 + A13*dy^2*px*z^2 + A8*dx*dy*z^2 + A18*dx^2*py^3*z^2 + A15*dy^2*px^3*z^2 + A14*dy^2*px^2*z^2 + A14*dx^2*py^2*z^2 + A3*dx^2*z^2 + A12*dy^2*z^2 + 4*A20*dy*px*py^3*z + 3*A17*dy*px*py^2*z + 2*A15*dy*px^3*py*z + 2*A14*dy*px^2*py*z + 2*A18*dx*px*py^3*z + 2*A14*dx*px*py^2*z + 4*A11*dx*px^3*py*z + 3*A10*dx*px^2*py*z + 2*A9*dx*px*py*z + 2*A13*dy*px*py*z + 3*A18*dy*px^2*py^2*z + 3*A15*dx*px^2*py^2*z + 2*A12*dy*py*z + 2*A3*dx*px*z + 5*A6*dx*px^4*z + 4*A5*dx*px^3*z + 5*A21*dy*py^4*z + 3*A4*dx*px^2*z + 4*A19*dy*py^3*z + 3*A16*dy*py^2*z + A8*dy*px*z + A8*dx*py*z + A9*dy*px^2*z + A20*dx*py^4*z + A17*dx*py^3*z + A13*dx*py^2*z + A11*dy*px^4*z + A10*dy*px^3*z + A7*dy*z + A2*dx*z - dz*z + A9*px^2*py + A20*px*py^4 + A17*px*py^3 + A13*px*py^2 + A11*px^4*py + A10*px^3*py + A8*px*py + A18*px^2*py^3 + A15*px^3*py^2 + A14*px^2*py^2 - pz + A6*px^5 + A5*px^4 + A4*px^3 + A21*py^5 + A3*px^2 + A19*py^4 + A16*py^3 + A12*py^2 + A7*py + A2*px + A1, z)
Is there any way to sort the roots in order to apply later the function "roots"? Or is there anyway to get the roots out of this Answer = RootOf(.......
I have all the variables at the end but they are quite a lot that's why I have to solve it generally before and then apply for loop to get the roots.
Many thanks in advance
This is the answer that no body could give it to me and I got vote down for it:
a = char(Answer); % Convert from symbolic to String
R = strrep(a,'RootOf(',''); % Preparing the Expression to be ready to get the Coefficients
R1 = strrep(R,', z)',''); % Preparing the Expression to be ready to get the Coefficients
b = sym(R1); % converting back to Symbolic
Coeff = coeffs(b,z); % I got the Coefficients