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.');
I'm trying to solve a system of 3 equations with 3 variables in matlab.
Here's the matlab code:
clear;
close all;
clc;
syms x y z
eq1 = 30 - ((30*sin(y) + (6032463447834141*(30*cos(y)*sin(x) - (30*sin(y)*((139651314883359*cos(y)*sin(x))/140737488355328 + (10*15869^(1/2)*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)))/15869 + (12*15869^(1/2)*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)))/15869))/((139651314883359*sin(y))/140737488355328 - (10*15869^(1/2)*cos(y)*cos(z))/15869 + (12*15869^(1/2)*cos(y)*sin(z))/15869)))/(72057594037927936*(((842441452475839123526592959619*cos(y)*sin(x))/10141204801825835211973625643008 + (30162317239170705*15869^(1/2)*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)))/571740979893939208192 + (18097390343502423*15869^(1/2)*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)))/285870489946969604096)/((139651314883359*sin(y))/140737488355328 - (10*15869^(1/2)*cos(y)*cos(z))/15869 + (12*15869^(1/2)*cos(y)*sin(z))/15869) + 3888617886914517/36028797018963968)))*((12*15869^(1/2)*(cos(z)*sin(x) + cos(x)*sin(y)*sin(z)))/15869 - (139651314883359*cos(x)*cos(y))/140737488355328 + (10*15869^(1/2)*(sin(x)*sin(z) - cos(x)*cos(z)*sin(y)))/15869))/((139651314883359*sin(y))/140737488355328 - (10*15869^(1/2)*cos(y)*cos(z))/15869 + (12*15869^(1/2)*cos(y)*sin(z))/15869) - 30*cos(x)*cos(y) == -(8922777075240197*(30*cos(y)*sin(x) - (30*sin(y)*((139651314883359*cos(y)*sin(x))/140737488355328 + (10*15869^(1/2)*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)))/15869 + (12*15869^(1/2)*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)))/15869))/((139651314883359*sin(y))/140737488355328 - (10*15869^(1/2)*cos(y)*cos(z))/15869 + (12*15869^(1/2)*cos(y)*sin(z))/15869)))/(9007199254740992*(((842441452475839123526592959619*cos(y)*sin(x))/10141204801825835211973625643008 + (30162317239170705*15869^(1/2)*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)))/571740979893939208192 + (18097390343502423*15869^(1/2)*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)))/285870489946969604096)/((139651314883359*sin(y))/140737488355328 - (10*15869^(1/2)*cos(y)*cos(z))/15869 + (12*15869^(1/2)*cos(y)*sin(z))/15869) + 3888617886914517/36028797018963968));
eq2 = ((30*sin(y) - (2751313666748167*(30*cos(y)*sin(x) + (30*sin(y)*((10*15869^(1/2)*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)))/15869 - (139651314883359*cos(y)*sin(x))/140737488355328 + (12*15869^(1/2)*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)))/15869))/((139651314883359*sin(y))/140737488355328 + (10*15869^(1/2)*cos(y)*cos(z))/15869 - (12*15869^(1/2)*cos(y)*sin(z))/15869)))/(36028797018963968*(((13756568333740835*15869^(1/2)*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)))/285870489946969604096 - (384224571217937317986432052953*cos(y)*sin(x))/5070602400912917605986812821504 + (8253941000244501*15869^(1/2)*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)))/142935244973484802048)/((139651314883359*sin(y))/140737488355328 + (10*15869^(1/2)*cos(y)*cos(z))/15869 - (12*15869^(1/2)*cos(y)*sin(z))/15869) - 1085710309166423/18014398509481984)))*((139651314883359*cos(x)*cos(y))/140737488355328 + (12*15869^(1/2)*(cos(z)*sin(x) + cos(x)*sin(y)*sin(z)))/15869 + (10*15869^(1/2)*(sin(x)*sin(z) - cos(x)*cos(z)*sin(y)))/15869))/((139651314883359*sin(y))/140737488355328 + (10*15869^(1/2)*cos(y)*cos(z))/15869 - (12*15869^(1/2)*cos(y)*sin(z))/15869) - 30*cos(x)*cos(y) + 30 == -(8964476491404667*(30*cos(y)*sin(x) + (30*sin(y)*((10*15869^(1/2)*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)))/15869 - (139651314883359*cos(y)*sin(x))/140737488355328 + (12*15869^(1/2)*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)))/15869))/((139651314883359*sin(y))/140737488355328 + (10*15869^(1/2)*cos(y)*cos(z))/15869 - (12*15869^(1/2)*cos(y)*sin(z))/15869)))/(9007199254740992*(((13756568333740835*15869^(1/2)*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)))/285870489946969604096 - (384224571217937317986432052953*cos(y)*sin(x))/5070602400912917605986812821504 + (8253941000244501*15869^(1/2)*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)))/142935244973484802048)/((139651314883359*sin(y))/140737488355328 + (10*15869^(1/2)*cos(y)*cos(z))/15869 - (12*15869^(1/2)*cos(y)*sin(z))/15869) - 1085710309166423/18014398509481984));
eq3 = 30 - ((30*sin(y) - (5869842550983837*(30*cos(y)*sin(x) + (30*sin(y)*((278913769878129*cos(y)*sin(x))/281474976710656 + (10*13469^(1/2)*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)))/13469 - (12*13469^(1/2)*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)))/13469))/((10*13469^(1/2)*cos(y)*cos(z))/13469 - (278913769878129*sin(y))/281474976710656 + (12*13469^(1/2)*cos(y)*sin(z))/13469)))/(72057594037927936*((5869842550983837*((278913769878129*cos(y)*sin(x))/281474976710656 + (10*13469^(1/2)*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)))/13469 - (12*13469^(1/2)*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)))/13469))/(72057594037927936*((10*13469^(1/2)*cos(y)*cos(z))/13469 - (278913769878129*sin(y))/281474976710656 + (12*13469^(1/2)*cos(y)*sin(z))/13469)) + 7567579956186207/72057594037927936)))*((278913769878129*cos(x)*cos(y))/281474976710656 + (12*13469^(1/2)*(cos(z)*sin(x) + cos(x)*sin(y)*sin(z)))/13469 - (10*13469^(1/2)*(sin(x)*sin(z) - cos(x)*cos(z)*sin(y)))/13469))/((10*13469^(1/2)*cos(y)*cos(z))/13469 - (278913769878129*sin(y))/281474976710656 + (12*13469^(1/2)*cos(y)*sin(z))/13469) - 30*cos(x)*cos(y) == (8927287469964123*(30*cos(y)*sin(x) + (30*sin(y)*((278913769878129*cos(y)*sin(x))/281474976710656 + (10*13469^(1/2)*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)))/13469 - (12*13469^(1/2)*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)))/13469))/((10*13469^(1/2)*cos(y)*cos(z))/13469 - (278913769878129*sin(y))/281474976710656 + (12*13469^(1/2)*cos(y)*sin(z))/13469)))/(9007199254740992*((5869842550983837*((278913769878129*cos(y)*sin(x))/281474976710656 + (10*13469^(1/2)*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)))/13469 - (12*13469^(1/2)*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)))/13469))/(72057594037927936*((10*13469^(1/2)*cos(y)*cos(z))/13469 - (278913769878129*sin(y))/281474976710656 + (12*13469^(1/2)*cos(y)*sin(z))/13469)) + 7567579956186207/72057594037927936));
equation_3_angles(1) = eq1;
equation_3_angles(2) = eq2;
equation_3_angles(3) = eq3;
fprintf('Trying to solve the equations (the function never finish)\n');
[x_solution, y_solution, z_solution] = solve(equation_3_angles,[x, y, z]);
The solve function never finishes.. So I can't get the solution.
There are solutions for the equations above.
The solutions are
(x,y,z)=(0,0,0)
(x,y,z)=(0.523598775598299, 0, 0)
But I need to get these solutions mathematically.
How can I do that?
Thanks
I tried to solve them numerically with this script:
x0=[0.1 0.1 0.1]
sol=fsolve(#fun,x0)
function[obj]=fun(xx)
Calling this function
function[obj]=fun(xx)
x=xx(1);
y=xx(2);
z=xx(3);
eq1 = 30 - ((30*sin(y) + (6032463447834141*(30*cos(y)*sin(x) - (30*sin(y)*((139651314883359*cos(y)*sin(x))/140737488355328 + (10*15869^(1/2)*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)))/15869 + (12*15869^(1/2)*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)))/15869))/((139651314883359*sin(y))/140737488355328 - (10*15869^(1/2)*cos(y)*cos(z))/15869 + (12*15869^(1/2)*cos(y)*sin(z))/15869)))/(72057594037927936*(((842441452475839123526592959619*cos(y)*sin(x))/10141204801825835211973625643008 + (30162317239170705*15869^(1/2)*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)))/571740979893939208192 + (18097390343502423*15869^(1/2)*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)))/285870489946969604096)/((139651314883359*sin(y))/140737488355328 - (10*15869^(1/2)*cos(y)*cos(z))/15869 + (12*15869^(1/2)*cos(y)*sin(z))/15869) + 3888617886914517/36028797018963968)))*((12*15869^(1/2)*(cos(z)*sin(x) + cos(x)*sin(y)*sin(z)))/15869 - (139651314883359*cos(x)*cos(y))/140737488355328 + (10*15869^(1/2)*(sin(x)*sin(z) - cos(x)*cos(z)*sin(y)))/15869))/((139651314883359*sin(y))/140737488355328 - (10*15869^(1/2)*cos(y)*cos(z))/15869 + (12*15869^(1/2)*cos(y)*sin(z))/15869) - 30*cos(x)*cos(y) -( -(8922777075240197*(30*cos(y)*sin(x) - (30*sin(y)*((139651314883359*cos(y)*sin(x))/140737488355328 + (10*15869^(1/2)*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)))/15869 + (12*15869^(1/2)*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)))/15869))/((139651314883359*sin(y))/140737488355328 - (10*15869^(1/2)*cos(y)*cos(z))/15869 + (12*15869^(1/2)*cos(y)*sin(z))/15869)))/(9007199254740992*(((842441452475839123526592959619*cos(y)*sin(x))/10141204801825835211973625643008 + (30162317239170705*15869^(1/2)*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)))/571740979893939208192 + (18097390343502423*15869^(1/2)*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)))/285870489946969604096)/((139651314883359*sin(y))/140737488355328 - (10*15869^(1/2)*cos(y)*cos(z))/15869 + (12*15869^(1/2)*cos(y)*sin(z))/15869) + 3888617886914517/36028797018963968)));
eq2 = ((30*sin(y) - (2751313666748167*(30*cos(y)*sin(x) + (30*sin(y)*((10*15869^(1/2)*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)))/15869 - (139651314883359*cos(y)*sin(x))/140737488355328 + (12*15869^(1/2)*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)))/15869))/((139651314883359*sin(y))/140737488355328 + (10*15869^(1/2)*cos(y)*cos(z))/15869 - (12*15869^(1/2)*cos(y)*sin(z))/15869)))/(36028797018963968*(((13756568333740835*15869^(1/2)*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)))/285870489946969604096 - (384224571217937317986432052953*cos(y)*sin(x))/5070602400912917605986812821504 + (8253941000244501*15869^(1/2)*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)))/142935244973484802048)/((139651314883359*sin(y))/140737488355328 + (10*15869^(1/2)*cos(y)*cos(z))/15869 - (12*15869^(1/2)*cos(y)*sin(z))/15869) - 1085710309166423/18014398509481984)))*((139651314883359*cos(x)*cos(y))/140737488355328 + (12*15869^(1/2)*(cos(z)*sin(x) + cos(x)*sin(y)*sin(z)))/15869 + (10*15869^(1/2)*(sin(x)*sin(z) - cos(x)*cos(z)*sin(y)))/15869))/((139651314883359*sin(y))/140737488355328 + (10*15869^(1/2)*cos(y)*cos(z))/15869 - (12*15869^(1/2)*cos(y)*sin(z))/15869) - 30*cos(x)*cos(y) + 30 -( -(8964476491404667*(30*cos(y)*sin(x) + (30*sin(y)*((10*15869^(1/2)*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)))/15869 - (139651314883359*cos(y)*sin(x))/140737488355328 + (12*15869^(1/2)*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)))/15869))/((139651314883359*sin(y))/140737488355328 + (10*15869^(1/2)*cos(y)*cos(z))/15869 - (12*15869^(1/2)*cos(y)*sin(z))/15869)))/(9007199254740992*(((13756568333740835*15869^(1/2)*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)))/285870489946969604096 - (384224571217937317986432052953*cos(y)*sin(x))/5070602400912917605986812821504 + (8253941000244501*15869^(1/2)*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)))/142935244973484802048)/((139651314883359*sin(y))/140737488355328 + (10*15869^(1/2)*cos(y)*cos(z))/15869 - (12*15869^(1/2)*cos(y)*sin(z))/15869) - 1085710309166423/18014398509481984)));
eq3 = 30 - ((30*sin(y) - (5869842550983837*(30*cos(y)*sin(x) + (30*sin(y)*((278913769878129*cos(y)*sin(x))/281474976710656 + (10*13469^(1/2)*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)))/13469 - (12*13469^(1/2)*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)))/13469))/((10*13469^(1/2)*cos(y)*cos(z))/13469 - (278913769878129*sin(y))/281474976710656 + (12*13469^(1/2)*cos(y)*sin(z))/13469)))/(72057594037927936*((5869842550983837*((278913769878129*cos(y)*sin(x))/281474976710656 + (10*13469^(1/2)*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)))/13469 - (12*13469^(1/2)*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)))/13469))/(72057594037927936*((10*13469^(1/2)*cos(y)*cos(z))/13469 - (278913769878129*sin(y))/281474976710656 + (12*13469^(1/2)*cos(y)*sin(z))/13469)) + 7567579956186207/72057594037927936)))*((278913769878129*cos(x)*cos(y))/281474976710656 + (12*13469^(1/2)*(cos(z)*sin(x) + cos(x)*sin(y)*sin(z)))/13469 - (10*13469^(1/2)*(sin(x)*sin(z) - cos(x)*cos(z)*sin(y)))/13469))/((10*13469^(1/2)*cos(y)*cos(z))/13469 - (278913769878129*sin(y))/281474976710656 + (12*13469^(1/2)*cos(y)*sin(z))/13469) - 30*cos(x)*cos(y) -( (8927287469964123*(30*cos(y)*sin(x) + (30*sin(y)*((278913769878129*cos(y)*sin(x))/281474976710656 + (10*13469^(1/2)*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)))/13469 - (12*13469^(1/2)*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)))/13469))/((10*13469^(1/2)*cos(y)*cos(z))/13469 - (278913769878129*sin(y))/281474976710656 + (12*13469^(1/2)*cos(y)*sin(z))/13469)))/(9007199254740992*((5869842550983837*((278913769878129*cos(y)*sin(x))/281474976710656 + (10*13469^(1/2)*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)))/13469 - (12*13469^(1/2)*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)))/13469))/(72057594037927936*((10*13469^(1/2)*cos(y)*cos(z))/13469 - (278913769878129*sin(y))/281474976710656 + (12*13469^(1/2)*cos(y)*sin(z))/13469)) + 7567579956186207/72057594037927936)));
obj=[eq1;eq2;eq3]
end
This is what I got, as expected
fsolve completed because the vector of function values is near zero
as measured by the default value of the function tolerance, and
the problem appears regular as measured by the gradient.
<stopping criteria details>
sol =
0.5236 0.0000 0.0000
If you use x0=[0 0 0] as seed, the result is (as expected):
sol =
0 0 0
But, the solution is extremely dependant of the initial guess (seed). For example, for x0=[1 1 1] the result is:
sol =
-2.2063 3.0245 3.1109
In order to find the possible solutions, you can modify the script to perform a sweep like this:
n=5; %You can change this value for trying more points
x0=linspace(0,2*pi,n)
y0=linspace(0,2*pi,n)
z0=linspace(0,2*pi,n)
sol=zeros(n^3,3);
fval=zeros(n^3,3);
fval_flag=ones(n^3,1);
c=0;
for i=1:n
for j=1:n
for k=1:n
c=c+1;
[sol(c,:),fval(c,:)]=fsolve(#fun,[x0(i) y0(j) z0(k)]);
%Check if the function evaluation is larger than 1e-6 because
%this might not be a solution and set a flag
if any(fval(c,:)>1e-6)
fval_flag(c)=0;
end
end
end
end
%Get only the solutions from 0 to 2*pi. This will list all the rows with
%this condition
[row,col]=find(sol<0 | sol>2*pi);
%Sort and get the unique rows with wrong values
row=unique(sort(row));
%Create a copy of the solution and remove this rows from the solution
sol2=sol;
sol2(row,:)=[];
fval_flag(row)=[];
%Show and compare the obtained solutions with its flag values (is the 4th
%column is 0 then it might not be a solution
out=[sol2 fval_flag]
This will remove the possible solutions outside the range [0 2*pi] and will show you the solutions for x,y,a and also a flag showing a 0 with values that are possibly not a solution because the equations are not close enought to 0 (I used a tolerance varue of 1e-6).
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) ];
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.
I would like to find the center of the circle that passes between two points A and B, knowing the (x,y) pairs of both points, as well as the circle radius.
This should produce two pairs of center candidates, [Xcenter1, Ycenter1] and [Xcenter2, Ycenter2], respectively. They correspond to the center of the circle to the left and to the right of segment [AB].
I have written the following symbolic Matlab script:
syms x1 y1 x2 y2 xc yc r
f1 = (x1-xc)^2 + (y1-yc)^2 - r^2;
f2 = (x2-xc)^2 + (y2-yc)^2 - r^2;
solve(f1,f2,'xc,yc')
The result of which is:
ans =
xc: [2x1 sym]
yc: [2x1 sym]
From this thread, the result should look like:
xc1 = x1/2 + x2/2 - (y1*(-(x1^2 - 4*r^2 - 2*x1*x2 + x2^2 + y1^2 - 2*y1*y2 + y2^2)/(x1^2 - 2*x1*x2 + x2^2 + y1^2 - 2*y1*y2 + y2^2))^(1/2))/2 + (y2*(-(x1^2 - 4*r^2 - 2*x1*x2 + x2^2 + y1^2 - 2*y1*y2 + y2^2)/(x1^2 - 2*x1*x2 + x2^2 + y1^2 - 2*y1*y2 + y2^2))^(1/2))/2
xc2 = -(2*y1*(y1/2 + y2/2 - (x1*(-(x1^2 - 4*r^2 - 2*x1*x2 + x2^2 + y1^2 - 2*y1*y2 + y2^2)/(x1^2 - 2*x1*x2 + x2^2 + y1^2 - 2*y1*y2 + y2^2))^(1/2))/2 + (x2*(-(x1^2 - 4*r^2 - 2*x1*x2 + x2^2 + y1^2 - 2*y1*y2 + y2^2)/(x1^2 - 2*x1*x2 + x2^2 + y1^2 - 2*y1*y2 + y2^2))^(1/2))/2) - 2*y2*(y1/2 + y2/2 - (x1*(-(x1^2 - 4*r^2 - 2*x1*x2 + x2^2 + y1^2 - 2*y1*y2 + y2^2)/(x1^2 - 2*x1*x2 + x2^2 + y1^2 - 2*y1*y2 + y2^2))^(1/2))/2 + (x2*(-(x1^2 - 4*r^2 - 2*x1*x2 + x2^2 + y1^2 - 2*y1*y2 + y2^2)/(x1^2 - 2*x1*x2 + x2^2 + y1^2 - 2*y1*y2 + y2^2))^(1/2))/2) - x1^2 + x2^2 - y1^2 + y2^2)/(2*x1 - 2*x2)
yc1 = y1/2 + y2/2 + (x1*(-(x1^2 - 4*r^2 - 2*x1*x2 + x2^2 + y1^2 - 2*y1*y2 + y2^2)/(x1^2 - 2*x1*x2 + x2^2 + y1^2 - 2*y1*y2 + y2^2))^(1/2))/2 - (x2*(-(x1^2 - 4*r^2 - 2*x1*x2 + x2^2 + y1^2 - 2*y1*y2 + y2^2)/(x1^2 - 2*x1*x2 + x2^2 + y1^2 - 2*y1*y2 + y2^2))^(1/2))/2
yc2 = y1/2 + y2/2 - (x1*(-(x1^2 - 4*r^2 - 2*x1*x2 + x2^2 + y1^2 - 2*y1*y2 + y2^2)/(x1^2 - 2*x1*x2 + x2^2 + y1^2 - 2*y1*y2 + y2^2))^(1/2))/2 + (x2*(-(x1^2 - 4*r^2 - 2*x1*x2 + x2^2 + y1^2 - 2*y1*y2 + y2^2)/(x1^2 - 2*x1*x2 + x2^2 + y1^2 - 2*y1*y2 + y2^2))^(1/2))/2
What could I be doing wrong? Thanks in advance for any suggestions.
If you call the function without storing it's output, Matlab stores the output automatically in a variable called ans. That variable is not reliable, because a subsequent call to another function with non-assigned outputs may override the value of ans.
In the specific case of solve, it has some ways of accessing it's output (see the documentation). By default it returns a struct containing the function variables (in this case, xc and yc). As no variable was assigned, the ans variable stored this struct.
Try calling solve this way:
[xc,yc] = solve(f1,f2,'xc,yc')
Now you have it's outputs (xc and yc) and can use them as you intended. You could also call the function this way:
S = solve(f1,f2,'xc,yc')
Then you'll have S.xc and S.yc, stored as a struct.