lsqnonlin with vector input and multiple equations.

5 vues (au cours des 30 derniers jours)
Emma Van Puyenbroeck
Emma Van Puyenbroeck le 21 Fév 2020
when I try to run my model I always receive the error 'Undefined function or variable 'x'.'
How should I specify my variable x?
function y=testst(T_1,T_2,P_c,Q_gen)
x_1=0.35
x_2=0.21
y_3=0.85
h_1=1500
h_2=500
h_3=230
f(1)=x(1)-x(2)-x(3);
f(2)=x_1*x(1)-x_2*x(2)-y_3*x(3);
f(3)=Q_gen+x(1)*h_1-x(2)*h_2-x(3)*h_3;
steadystate=@(x) f(x);
st=[0.005 ;0.005; 0.005]';
lb=[0; 0 ;0];
ub=[1 ;1 ;1];
x=lsqnonlin('steadystate',st,lb,ub)
y=x
end
I also tried this:
st=[0.005 ;0.005;0.005];
lb=[0; 0 ;0];
ub=[1 ;1;1];
y=lsqnonlin(@(x) steadystate(x,x_1,y_3,h_1,h_2,h_3,Q_gen),st,lb,ub);
function f= steadystate(x,x_1,x_2,y_3,h_1,h_2,h_3,Q_gen)
f(1)=x(1)-x(2)-x(3);
f(2)=x_1*x(1)-x_2*x(2)-y_3*x(3);
f(3)=Q_gen+x(1)*h_1-x(2)*h_2-x(3)*h_3;
end
but than I had 'not enough input arguments'

Réponses (1)

Pravin Jagtap
Pravin Jagtap le 24 Fév 2020
Hello Emma,
The error mentioned is caused because of the way 'function handle' is used (steadystate=@(x) f(x);). In your case, it is better to write the separate function which returns 'f(1)', 'f(2)' and f(3) values. Please refer to the following example for creating the function handle:
function f = get_f(x)
f(1) = x(1);
f(2) = x(2);
f(3) = x(3);
end
Then, run the following script to see how to create the function handle:
f = @get_f;
x = [1 2 3];
steadystate = f(x)
Here, ‘steadystate’ will be 1x3 vector. Modify your code using above reference and for more details on how to use function handles use the following documentation link:

Catégories

En savoir plus sur Numerical Integration and Differentiation dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by