How to use fsolve with input from an array

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 Kommentare

KALYAN ACHARJYA
KALYAN ACHARJYA am 23 Aug. 2020
Bearbeitet: KALYAN ACHARJYA am 23 Aug. 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 ??
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.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Alan Stevens
Alan Stevens am 23 Aug. 2020

0 Stimmen

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 Kommentar

Thanks for your answer, but I need it to be solved with fsolve

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Line Plots finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by