How to use fsolve with input from an array
Mostrar comentarios más antiguos
Hi, I wrote down a simple code, I want to solve it by "fsolve" but with applying an array of values instead of one value for "t", I do not know how I can do it, please help. Thanks in advance.
function y=func(x)
y(1)=3*x(1)+x(2)-t;
y(2)=x(1)-2*x(2)-3;
end
t is an array of [4 5 6]
Actually I simplified my main problem. So, this is my main code:
function F = func(x)
tetha=0.2;
F=zeros(3,1);
F(1,1) = x(1)+(tetha/(1-tetha))*x(2)-(0.21/(1-tetha));
F(2,1) = x(3)-(x(2)*(1-x(1))/(x(1)*(1-x(2))));
F(3,1) = x(3)-(2.98*((x(1)*(x(3)-1)+1-0.1*x(3))/(x(1)*(x(3)-1)+1)-0.1));
end
And here it is its solver:
clear
clc
fun = @func;
x0 = [0.5,0.5,1];
x(:,1)=fsolve(fun,x0);
disp(x)
It is ok with one value for tetha, but the problem is that tetha is an array of [0.2 0.4 0.6 0.8]. In fact, I need this system of equations to be solved 4 times (length of tetha) for x(1), x(2) , and x(3) and for each time output be displayed.
2 comentarios
KALYAN ACHARJYA
el 23 de Ag. de 2020
Editada: KALYAN ACHARJYA
el 23 de Ag. de 2020
function y=func(x)
y(1)=3*x(1)+x(2)-t;
y(2)=x(1)-2*x(2)-3;
end
t is not there in the input argument list??
y=func(x)
Or sorry I did not understand the question properly, can you restructure the question again, y(2) independent with t ??
Amirreza Reyhanifar
el 23 de Ag. de 2020
Respuestas (1)
Alan Stevens
el 23 de Ag. de 2020
Presumably you want to find x(1) and x(2) given values for y(1) and y(2). If so, then you can simply solve them as follows:
t = [4 5 6];
M = [3 1; 1 -2];
y0 = [6; 7]; % replace with your own values
for i = 1:3
y = y0 + [t(i); 3];
x(:,i) = M\y;
end
giving results like
x =
4.2857 4.5714 4.8571
-2.8571 -2.7143 -2.5714
where each column gives x(1) and x(2) for the various values of t.
1 comentario
Amirreza Reyhanifar
el 23 de Ag. de 2020
Categorías
Más información sobre Line Plots en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!