fsolve for non linear equation

function F=Almalki_HW2_P3(x)
global v V k1 k2 CAin CBin CCin CDin
v=1; V=100; k1=1; k2=1; CAin=1; CBin=2; CCin=0; CDin=0;
F=[v*(CAin-x(1))+V*(-k1*x(1)*x(2));
v*(CBin-x(2))+V*(-k1*x(1)*x(2)-k2*x(1)*x(2));
v*(CCin-x(3))+V*(k1*x(1)*x(2)-k2*x(3)*x(2));
v*(CDin-x(4))+V*(k2*x(3)*x(2))];
x0=[ 0; 0; 0; 0];
options=optimoptions('fsolve','Display','iter');
[x,fval]=fsolve(@Almalki_HW2_P3,x0,options)
end
I typed those code for non linear equations, but when I run the codes, I got this message "Not enough input arguments."
So, do you have any idea
thanks

Respuestas (1)

Star Strider
Star Strider el 18 de Feb. de 2020
One problem is that you called fsolve inside the function. That is not appropriate. The other is that you use global variables.
Try this instead:
function F=Almalki_HW2_P3(x, v, V, k1, k2, CAin, CBin, CCin, CDin)
v=1; V=100; k1=1; k2=1; CAin=1; CBin=2; CCin=0; CDin=0;
F=[v*(CAin-x(1))+V*(-k1*x(1)*x(2));
v*(CBin-x(2))+V*(-k1*x(1)*x(2)-k2*x(1)*x(2));
v*(CCin-x(3))+V*(k1*x(1)*x(2)-k2*x(3)*x(2));
v*(CDin-x(4))+V*(k2*x(3)*x(2))];
end
x0=[ 0; 0; 0; 0]+eps;
options=optimoptions('fsolve','Display','iter');
[x,fval]=fsolve(@(x)Almalki_HW2_P3(x, v, V, k1, k2, CAin, CBin, CCin, CDin),x0,options)
I assume here that the other arguments exist in your workspace. I created random scalars for them to test this code. It runs without error.

5 comentarios

Faisal Al-malki
Faisal Al-malki el 19 de Feb. de 2020
Thanks for your reply. I copied the same code that you provided me with but unfortunatelly it does not work and shows this message below:
Error: File: Almalki_HW2_P3.m Line: 8 Column: 1
This statement is not inside any function.
(It follows the END that terminates the definition of the function "Almalki_HW2_P3".)
Star Strider
Star Strider el 19 de Feb. de 2020
The code I posted ran without error, or I would not have posted it.
I have no idea what the problem is, or why it threw the error you posted.
Faisal Al-malki
Faisal Al-malki el 19 de Feb. de 2020
so, any one have any idea how to overcome this issue.
Faisal Al-malki
Faisal Al-malki el 19 de Feb. de 2020
or could you send me the matlab file of this code on my email (fanwsa@gmail.com)
Star Strider
Star Strider el 19 de Feb. de 2020
Save the function on your MATLAB search path as:
Almalki_HW2_P3.m
Then run the fsolve code.

La pregunta está cerrada.

Productos

Etiquetas

Preguntada:

el 18 de Feb. de 2020

Cerrada:

el 20 de Ag. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by