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

回答(1 个)

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 个评论

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".)
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.
so, any one have any idea how to overcome this issue.
or could you send me the matlab file of this code on my email (fanwsa@gmail.com)
Save the function on your MATLAB search path as:
Almalki_HW2_P3.m
Then run the fsolve code.

此问题已关闭。

产品

标签

关闭:

2021-8-20

Community Treasure Hunt

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

Start Hunting!

Translated by