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