Matlab: Nonlinear Eq: error - matlab

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) ];

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.');

Double sum and how to make a matrix to plot in 3D? (surface plot)

Edit
To clarify. I have a function f that takes in 5 arguments z,x,y,n,m. The order of events should go as follows:
Upon calling the function test, the variable z is assigned, say z = 1.
A linear combination is created by adding f with with each of elements of ni inserted and the result is stored in fn, as such (z = 1, so no more z variable):
fn = x + 2.*y + exp(0) - sqrt(m) +
x + 2.*y + exp(2) - sqrt(m) +
x + 2.*y + exp(4) - sqrt(m) +
x + 2.*y + exp(6) - sqrt(m) +
x + 2.*y + exp(8) - sqrt(m) +
x + 2.*y + exp(10) - sqrt(m) =
6*x + 12*y + 1 + exp(2) + exp(4) + exp(6) + exp(8) + exp(10) - 6*sqrt(m) =
6*x + 12*y + 25473.8 - 6*sqrt(m)
A linear combination is created by adding fn with each of elements of mi inserted and the result is stored in fnm. (I don't know how to do 1. and 2. simultaneously. If you do, please let me know):
fnm = 6*x + 12*y + 1 + exp(2) + exp(4) + exp(6) + exp(8) + exp(10) - 6*sqrt(0) +
6*x + 12*y + 1 + exp(2) + exp(4) + exp(6) + exp(8) + exp(10) - 6*sqrt(2) +
6*x + 12*y + 1 + exp(2) + exp(4) + exp(6) + exp(8) + exp(10) - 6*sqrt(4) +
6*x + 12*y + 1 + exp(2) + exp(4) + exp(6) + exp(8) + exp(10) - 6*sqrt(6) +
6*x + 12*y + 1 + exp(2) + exp(4) + exp(6) + exp(8) + exp(10) - 6*sqrt(8) +
6*x + 12*y + 1 + exp(2) + exp(4) + exp(6) + exp(8) + exp(10) - 6*sqrt(10) =
6*x + 12*y + 25453.1
A surface is plotted by plugging in x and y from arrays xi and yi into fnm
and of Edit
I'm having trouble with the double summation over the function f. I tried to follow the example in the last answer presented here, but it's not working for some reason. Because my variables x and y are not defined beforehand, I included them into #() in the arrayfun. I was first getting the error Z must be a matrix, not a scalar or vector. , so I changed the function handle fnm to a symbolic function after reading about it here. But now the whole thing exploded ... I don't know what's going on. Why is it saying there aren't enough inputs? fn should only be a function of x, y, m on line 11 since z is already defined to be some number (lets say 1) and n's just got summed over.
function test(z)
f = #(z,x,y,n,m) z.*x + 2.*y + exp(n) - sqrt(m);
function s(z)
ni = 0:2:10;
mi = 0:2:10;
xi = -5:5;
yi = -5:5;
fn = #(n) arrayfun(#(z, x, y, ni, m) sum(f(z, x, y, ni, m)),n);
fnm = #(m) arrayfun(#(x, y, mi) sum(fn(x, y, mi)),m);
zz = sym(fnm);
[xx,yy] = meshgrid(xi,yi);
surf(xx,yy,zz)
end
s
end
so many errors :(
Error using test2>#(x,y,mi)sum(fn(x,y,mi)) (line 11)
Not enough input arguments.
Error in test2>#(m)arrayfun(#(x,y,mi)sum(fn(x,y,mi)),m) (line 11)
fnm = #(m) arrayfun(#(x, y, mi) sum(fn(x, y, mi)),m);
Error in sym>funchandle2ref (line 1209)
S = x(S{:});
Error in sym>tomupad (line 1114)
x = funchandle2ref(x);
Error in sym (line 151)
S.s = tomupad(x);
Error in test2/s (line 12)
zz = sym(fnm);
Error in test2 (line 18)
s
Are you trying to do this?. I am not optimizing anything, for you to understand it clearly....
function test(zi)
syms z x y n m fn fnm;
f0=symfun(z.*x + 2.*y + exp(n) - sqrt(m),[z x y n m]);
z=zi;
ni=0:2:10;
mi=0:2:10;
fn=0;
for i=1:length(ni);
fn=symfun(fn+f0(z,x,y,ni(i),m),[x y m]);
end
fnm=0;
for i=1:length(mi)
fnm=symfun(fnm+fn(x,y,mi(i)),[x y]);
end
xi=-5:5;
yi=-5:5;
for i=1:length(xi)
for j=1:length(yi)
zz(i,j)=eval(fnm(xi(i),yi(j)));
end
end
[xx,yy]=meshgrid(xi,yi);
mesh(xx,yy,zz);
So, test(1) produces this:
Beware of the symfun calls, which shall be put there in order every summation keeps onto a "symbolic" framework. The arrayfun could be used, though they are neat, they could be not necesarily efficient... Let that exercise as homework!! XD..

how can I solve a system of ODEs with some terminal conditions in matlab?

I have 14 first order differential equations.
14 conditions, 7 are initial ones like x1(0)=0, x2(0)=5...
7 are terminal ones x8(10)=25,x9(10)=0....
I think I should use bvp4c
I found this answer but I'm still have a couple of problems:how to solve a system of Ordinary Differential Equations (ODE's) in Matlab
I create a matlab function to put my system in.
x'=2x
y'=3x+5y
coding it like:
xdot=[2x(1);3x(1)+5x(2)]
like I would do in ode45.
Then I should do the same for the boundary conditions. But I have no idea on how to code them.
I should build a matrix containing them, but I haven't understood how to build it.
I'm trying to use this reference: http://www.math.tamu.edu/~phoward/m442/matode.pdf pag 12, but he does the y2=y' thing and I'm kind lost in my case. also it doesn't explain well how should I put the 14 conditions I have. 7 on one line and 7 on the other? how do I tell the program which value is referred to each variable?
thanks in advance.
here's the actual system. it's a bit huge, so I fear I need numerical methods.
f1=(delta1*gn-(beta*phi*x(7)*x(1)+(1-u1))/(x(1)+x(2)+x(3)+x(4))-mu*x(1)+psi*x(4));
f2=((beta*phi*x(7)*x(1)*(1-u1))/(x(1)+x(2)+x(3)+x(4))-d*x(2)-mu*x(2));
f3=(d*x(2)-(r+r0*u2)*x(3)-(alfa+mu)*x(3));
f4=((r+r0*u2)*x(3)-(mu+phi)*x(4));
f5=(delta2*hp-(phi*teta*x(3)*x(5)*(1-u1))/(x(1)+x(2)+x(3)+x(4))-gamma*x(5));
f6=((phi*teta*x(3)*x(5)*(1-u1))/(x(1)+x(2)+x(3)+x(4))-gamma*x(6)-k*x(6));
f7=(k*x(6)-gamma*x(7));
f8=x(8)*(mu - (beta*phi*x(1)*x(7) - u1 + 1)/(x(1) + x(2) + x(3) + x(4))^2 + (beta*phi*x(7))/(x(1) + x(2) + x(3) + x(4))) + x(9)*((beta*phi*x(7)*(u1 - 1))/(x(1) + x(2) + x(3) + x(4)) - (beta*phi*x(1)*x(7)*(u1 - 1))/(x(1) + x(2) + x(3) + x(4))^2) + (teta*x(12)*phi*x(3)*x(5)*(u1 - 1))/(x(1) + x(2) + x(3) + x(4))^2 - (teta*x(13)*phi*x(3)*x(5)*(u1 - 1))/(x(1) + x(2) + x(3) + x(4))^2;
f9=x(9)*(d + mu - (beta*phi*x(1)*x(7)*(u1 - 1))/(x(1) + x(2) + x(3) + x(4))^2) - d*x(10) - A1 - (x(8)*(beta*phi*x(1)*x(7) - u1 + 1))/(x(1) + x(2) + x(3) + x(4))^2 + (teta*x(12)*phi*x(3)*x(5)*(u1 - 1))/(x(1) + x(2) + x(3) + x(4))^2 - (teta*x(13)*phi*x(3)*x(5)*(u1 - 1))/(x(1) + x(2) + x(3) + x(4))^2;
f10= x(10)*(alfa + mu + r + r0*u2) - A2 - x(11)*(r + r0*u2) - x(12)*((teta*phi*x(5)*(u1 - 1))/(x(1) + x(2) + x(3) + x(4)) - (teta*phi*x(3)*x(5)*(u1 - 1))/(x(1) + x(2) + x(3) + x(4))^2) + x(13)*((teta*phi*x(5)*(u1 - 1))/(x(1) + x(2) + x(3) + x(4)) - (teta*phi*x(3)*x(5)*(u1 - 1))/(x(1) + x(2) + x(3) + x(4))^2) - (x(8)*(beta*phi*x(1)*x(7) - u1 + 1))/(x(1) + x(2) + x(3) + x(4))^2 - (beta*x(9)*phi*x(1)*x(7)*(u1 - 1))/(x(1) + x(2) + x(3) + x(4))^2;
f11=x(11)*(mu + phi) - x(8)*(psi + (beta*phi*x(1)*x(7) - u1 + 1)/(x(1) + x(2) + x(3) + x(4))^2) - (beta*x(9)*phi*x(1)*x(7)*(u1 - 1))/(x(1) + x(2) + x(3) + x(4))^2 + (teta*x(12)*phi*x(3)*x(5)*(u1 - 1))/(x(1) + x(2) + x(3) + x(4))^2 - (teta*x(13)*phi*x(3)*x(5)*(u1 - 1))/(x(1) + x(2) + x(3) + x(4))^2;
f12=x(12)*(gamma - (teta*phi*x(3)*(u1 - 1))/(x(1) + x(2) + x(3) + x(4))) + (teta*x(13)*phi*x(3)*(u1 - 1))/(x(1) + x(2) + x(3) + x(4));
f13=x(13)*(gamma + k) - k*x(14);
f14=gamma*x(14) + (beta*x(8)*phi*x(1))/(x(1) + x(2) + x(3) + x(4)) + (beta*x(9)*phi*x(1)*(u1 - 1))/(x(1) + x(2) + x(3) + x(4));
extra:
u1=max(a1,min(b1,1/(2*B1)*(beta*phi/(x(1)+x(2)+x(3)+x(4))*x(7)*x(1)*(x(9)-x(8))+phi*teta/(x(1)+x(2)+x(3)+x(4))*x(3)*x(5)*(x(13)-x(12)))));
u2=max(a2,min(b2,1/(2*B2)*(r0*x(3)*x(10)-r0*x(3)*x(11))));
To solve BVP ode's using syms, the ode is y''+3 y' + 3 y = 0, it is first converted to 2 first order (state space formulation) and then solved
clear all; close all
syms x(t) y(t)
Dx = diff(x);
Dy = diff(y);
eq1 = Dx == y;
eq2 = Dy == -3*x-5*y;
[x,y] = dsolve(eq1,eq2, x(0) == 0, y(1) ==1)
figure;
ezplot(x,[0,6])
To solve same BVP using bvp4c
clear all
t0 = 0; %initial time
tf = 6; %final time
odefun=#(t,y) [y(2); -3*y(1)-5*y(2)];
bcfun=#(yleft,yright) [yleft(1);yright(1)-1];
solinit = bvpinit(linspace(t0,tf),[0 1]);
sol = bvp4c(odefun,bcfun,solinit);
figure;
plot(sol.x(1,:),-sol.y(1,:),'r')
title('solution');
xlabel('time');
ylabel('y(t)');
grid;
ps. the numerical solution y-axis value ticks seems not to match the syms. But It looks this is just scaling of value thing. No time to look into it. May be someone can spot something and I'll update.

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)

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