fsolve doesn't give the expected answer

2 visualizaciones (últimos 30 días)
Federico MegaMan
Federico MegaMan el 12 de Nov. de 2019
Comentada: Federico MegaMan el 13 de Nov. de 2019
Hello,
I done (with the help of a member in another post) a fsolve script. It works but it doesn't give the expected result, or else the output spikes a lot.
Before I create this post i tried to solve my problem with other discussions and I read that there are more solution for trascendental equation (like mine). But I don't uderstand how to solve my problem.
This is my code, i have 12 equation and 13 unknows. I have to impose one of the unknows (T21) and it varies. After i have to calcolate all other 12 unknows while T21 varies.
T21=zeros(1,36);
j=0;
for t=0:0.1:2.755
j=j+1;
T21(j)=0.333*cos(t)-0.245;
end
sol = zeros(12,numel(T21));
for k = 1:numel(T21)
% the results are saved in a row 1...12
% every value of T21 match a column
if T21<=0
sol(:,k) = fsolve(@(x)funzmia(x,T21(k)), 1:12);
else
sol(:,k) = fsolve(@(x)funzmia2(x,T21(k)), 1:12);
end
end
figure;
plot(1:1:28, sol);
title('sol in funzione di t');
xlabe1=('t');
ylabe1=('sol');
grid on;
function F=funzmia(x,T21)
T11=x(1);
T12=x(2);
T13=x(3);
T14=x(4);
T15=x(5);
T22=x(6);
T23=x(7);
r23=x(8);
T31=x(9);
T32=x(10);
T33=x(11);
r32=x(12);
%T21=0
F(1)=(5^(0.5))*cos(T11)+cos(T12)+4*cos(T13)+cos(T14)+(5^(0.5))*cos(T15)-8;
F(2)=(5^(0.5))*sin(T11)+sin(T12)+4*sin(T13)+sin(T14)+(5^(0.5))*sin(T15);
F(3)=cos(T21)+3*cos(T22)+r23*cos(T23);
F(4)=sin(T21)+3*sin(T22)+r23*sin(T23);
F(5)=cos(T31)+r32*cos(T32)+3*cos(T33);
F(6)=sin(T31)+r32*sin(T32)+3*sin(T33);
F(7)=T12-T21;
F(8)=T31-T14;
F(9)=T13+T22-pi/2;
F(10)=T33-T22-pi;
F(11)=r23-(1+9+6*sin(T12-T13))^(0.5);
F(12)=r32-(1+9+6*cos(T14-T33))^(0.5);
end
function F=funzmia2(x,T21)
T11=x(1);
T12=x(2);
T13=x(3);
T14=x(4);
T15=x(5);
T22=x(6);
T23=x(7);
r23=x(8);
T31=x(9);
T32=x(10);
T33=x(11);
r32=x(12);
%T21=0
F(1)=(5^(0.5))*cos(T11)+cos(T12)+4*cos(T13)+cos(T14)+(5^(0.5))*cos(T15)-8;
F(2)=(5^(0.5))*sin(T11)+sin(T12)+4*sin(T13)+sin(T14)+(5^(0.5))*sin(T15);
F(3)=cos(T21)+3*cos(T22)+r23*cos(T23);
F(4)=sin(T21)+3*sin(T22)+r23*sin(T23);
F(5)=cos(T31)+r32*cos(T32)+3*cos(T33);
F(6)=sin(T31)+r32*sin(T32)+3*sin(T33);
F(7)=T12-T21;
F(8)=T31-T14;
F(9)=T13+T22-3*pi/2;
F(10)=T33-T22-pi;
F(11)=r23-(1+9+6*sin(T12-T13))^(0.5);
F(12)=r32-(1+9+6*cos(T14-T33))^(0.5);
end

Respuestas (4)

Nadir Altinbas
Nadir Altinbas el 12 de Nov. de 2019
%T21=0
this line should be
T21 =0;
  2 comentarios
Walter Roberson
Walter Roberson el 12 de Nov. de 2019
No, the value is passed in as a parameter.
Federico MegaMan
Federico MegaMan el 12 de Nov. de 2019
Exactly, the value of T21 changes and it is described by the for.

Iniciar sesión para comentar.


Nadir Altinbas
Nadir Altinbas el 13 de Nov. de 2019
error: 'funzmia2' undefined near line 7 column 29
error: __plt2vm__: matrix dimensions must match error: called from __plt__>__plt2vm__ at line 419 column 5 __plt__>__plt2__ at line 250 column 14 __plt__ at line 113 column 17 plot at line 223 column 10
  1 comentario
Federico MegaMan
Federico MegaMan el 13 de Nov. de 2019
Mmh it works on my pc, the only error i found was the precalling of T21 (1,28 instead of 1,36).
Maybe it depend by the matlab version(i have the 2018)?
Try again now.
T21=zeros(1,28);
j=0;
for t=0:0.1:2.755
j=j+1;
T21(j)=0.333*cos(t)-0.245;
end
sol = zeros(12,numel(T21));
for k = 1:numel(T21)
%the results for x are saved in rows 1...12
% every value of T21 match a column
if T21<=0
sol(:,k) = fsolve(@(x)funzmia(x,T21(k)), 1:12);
else
sol(:,k) = fsolve(@(x)funzmia2(x,T21(k)), 1:12);
end
end
figure;
plot(1:1:28, sol);
title('sol in funzione di t');
xlabe1=('t');
ylabe1=('sol');
grid on;
function F=funzmia(x,T21)
T11=x(1);
T12=x(2);
T13=x(3);
T14=x(4);
T15=x(5);
T22=x(6);
T23=x(7);
r23=x(8);
T31=x(9);
T32=x(10);
T33=x(11);
r32=x(12);
%T21=0
F(1)=(5^(0.5))*cos(T11)+cos(T12)+4*cos(T13)+cos(T14)+(5^(0.5))*cos(T15)-8;
F(2)=(5^(0.5))*sin(T11)+sin(T12)+4*sin(T13)+sin(T14)+(5^(0.5))*sin(T15);
F(3)=cos(T21)+3*cos(T22)+r23*cos(T23);
F(4)=sin(T21)+3*sin(T22)+r23*sin(T23);
F(5)=cos(T31)+r32*cos(T32)+3*cos(T33);
F(6)=sin(T31)+r32*sin(T32)+3*sin(T33);
F(7)=T12-T21;
F(8)=T31-T14;
F(9)=T13+T22-pi/2;
F(10)=T33-T22-pi;
F(11)=r23-(1+9+6*sin(T12-T13))^(0.5);
F(12)=r32-(1+9+6*cos(T14-T33))^(0.5);
end
function F=funzmia2(x,T21)
T11=x(1);
T12=x(2);
T13=x(3);
T14=x(4);
T15=x(5);
T22=x(6);
T23=x(7);
r23=x(8);
T31=x(9);
T32=x(10);
T33=x(11);
r32=x(12);
%T21=0
F(1)=(5^(0.5))*cos(T11)+cos(T12)+4*cos(T13)+cos(T14)+(5^(0.5))*cos(T15)-8;
F(2)=(5^(0.5))*sin(T11)+sin(T12)+4*sin(T13)+sin(T14)+(5^(0.5))*sin(T15);
F(3)=cos(T21)+3*cos(T22)+r23*cos(T23);
F(4)=sin(T21)+3*sin(T22)+r23*sin(T23);
F(5)=cos(T31)+r32*cos(T32)+3*cos(T33);
F(6)=sin(T31)+r32*sin(T32)+3*sin(T33);
F(7)=T12-T21;
F(8)=T31-T14;
F(9)=T13+T22-3*pi/2;
F(10)=T33-T22-pi;
F(11)=r23-(1+9+6*sin(T12-T13))^(0.5);
F(12)=r32-(1+9+6*cos(T14-T33))^(0.5);
end

Iniciar sesión para comentar.


Nadir Altinbas
Nadir Altinbas el 13 de Nov. de 2019
if T21<=0
funzmia(T21)<=0;
???
  3 comentarios
Nadir Altinbas
Nadir Altinbas el 13 de Nov. de 2019
i calculated the data is as following;
funzmiaT21 = [0.088000, 0.086336, 0.081362, 0.073127, 0.061713, 0.047235, 0.029837, 0.0096924, -0.012997, -0.038004
syms var funzmia,funzmiab;
funzmia(T21) = zeros(1,28);
j=0;
for t=0:0.1:2.755
j=j+1;
funzmiaT21(j)=0.333*cos(t)-0.245;
end
sol = zeros(12,numel(T21));
for k = 1:numel(T21)
%the results for x are saved in rows 1...12
% every value of T21 match a column
funzmia(T21)<=0
sol(:,k) = fsolve(@(x)funzmia(x,T21(k)), 1:12);
sol(:,k) = fsolve(@(x)funzmiab(x,T21(k)), 1:12);
end
figure;
plot(1:1:28, sol);
title('sol in funzione di t');
xlabe1=('t');
ylabe1=('sol');
grid on;
function F=funzmia(x,T21)
T11=x(1);
T12=x(2);
T13=x(3);
T14=x(4);
T15=x(5);
T22=x(6);
T23=x(7);
r23=x(8);
T31=x(9);
T32=x(10);
T33=x(11);
r32=x(12);
%T21=0
F(1)=(5^(0.5))*cos(T11)+cos(T12)+4*cos(T13)+cos(T14)+(5^(0.5))*cos(T15)-8;
F(2)=(5^(0.5))*sin(T11)+sin(T12)+4*sin(T13)+sin(T14)+(5^(0.5))*sin(T15);
F(3)=cos(T21)+3*cos(T22)+r23*cos(T23);
F(4)=sin(T21)+3*sin(T22)+r23*sin(T23);
F(5)=cos(T31)+r32*cos(T32)+3*cos(T33);
F(6)=sin(T31)+r32*sin(T32)+3*sin(T33);
F(7)=T12-T21;
F(8)=T31-T14;
F(9)=T13+T22-pi/2;
F(10)=T33-T22-pi;
F(11)=r23-(1+9+6*sin(T12-T13))^(0.5);
F(12)=r32-(1+9+6*cos(T14-T33))^(0.5);
end
function F=funzmiab(x,T21)
T11=x(1);
T12=x(2);
T13=x(3);
T14=x(4);
T15=x(5);
T22=x(6);
T23=x(7);
r23=x(8);
T31=x(9);
T32=x(10);
T33=x(11);
r32=x(12);
%T21=0
F(1)=(5^(0.5))*cos(T11)+cos(T12)+4*cos(T13)+cos(T14)+(5^(0.5))*cos(T15)-8;
F(2)=(5^(0.5))*sin(T11)+sin(T12)+4*sin(T13)+sin(T14)+(5^(0.5))*sin(T15);
F(3)=cos(T21)+3*cos(T22)+r23*cos(T23);
F(4)=sin(T21)+3*sin(T22)+r23*sin(T23);
F(5)=cos(T31)+r32*cos(T32)+3*cos(T33);
F(6)=sin(T31)+r32*sin(T32)+3*sin(T33);
F(7)=T12-T21;
F(8)=T31-T14;
F(9)=T13+T22-3.*pi/2;
F(10)=T33-T22-pi;
F(11)=r23-(1+9+6*sin(T12-T13))^(0.5);
F(12)=r32-(1+9+6*cos(T14-T33))^(0.5);
ans:
double('ans');
end
Federico MegaMan
Federico MegaMan el 13 de Nov. de 2019
It doesn't work if i don't modify something(precalling from funzmia(T21) to funzmiaT21, ans:, and delete funzmia(T21)<=0) and when i do it the solution it's the same. Am i wronging something?
Anyway i didn't understad why you didn't use "if" to say when T21>=0 use funzmia and when T21<0 use funzmia2.

Iniciar sesión para comentar.


Nadir Altinbas
Nadir Altinbas el 13 de Nov. de 2019
because of ' ELSE' error on it
  1 comentario
Federico MegaMan
Federico MegaMan el 13 de Nov. de 2019
Ah ok. Anyway if i use that code it doesn't work. I don't know if we have a different version or I'm wronging something.

Iniciar sesión para comentar.

Categorías

Más información sobre Historical Contests en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by