Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Not able to call mFile from command window.

Subject: Not able to call mFile from command window.

From: Zaki

Date: 18 Jun, 2012 10:18:06

Message: 1 of 2

Hi

I'm in the process of learning fsolve and jacobian. I read this mFile in a note and tried it out, wanting to learn from there. However, I cannot manage to call the mFile from the command window. I tried giving name to the Mfile as main.m and other tries as well, but still when I call the mFile, it won't run in the command window and will result to error. I also searched in the newsreader and google it but I cannot find how to call this mFile.

I appreciate anybody who can help me how to call the mFile. Following is the mFile.



%MAIN FUNCTION
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function main
global T
global E
global Stoichiometry
global P_Po
%set the inlet flows [CH4;CO;CO2;H2;H2O]
Nin = [10;0;0;0;1];
%set the temperature (K)
T = 900;
%set the pressure (Po = 1 bar)
P_Po = 1;
%set the stiociometry matrix
Stoichiometry = [ 1, 1, 1, 0, 0;...
4, 0, 0, 2, 2;...
0, 1, 2, 0, 1];
%Calculate the flows of elements entering the system
E = Stoichiometry*Nin;
%solve the system of equations
[N] = NewtonReduced(@f,@jac,[1,1,1,1,1]',100*eps);
%print the solution
disp(N)
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Function that computes the Jacobian
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [Jac] = jac(N)
global Stoichiometry
Jac(1:3,:) = Stoichiometry;
%row 4 (for the steam reforming reaction)
Jac(4,1) = -1/N(1) - 1/sum(N);
Jac(4,2) = 0 - 1/sum(N);
Jac(4,3) = 1/N(3) - 1/sum(N);
Jac(4,4) = 3/N(4) - 1/sum(N);
Jac(4,5) = -2/N(5) - 1/sum(N);
%row 5 (for the water-gas shift)
Jac(5,1) = 0;
Jac(5,2) = +1/N(2);
Jac(5,3) = -1/N(3);
Jac(5,4) = -1/N(4);
Jac(5,5) = +1/N(5);
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Function that computes equilibrium constants
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [LnKp1,LnKp2] = LnKp(T)
LnKp1 = 2.616e-5*T^2 - 7.491e-2*T + 6.251e1;
LnKp2 = -6.095e-6*T^2 + 1.692e-2*T - 1.116e1;
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Function that computes the function values (set of equations to be solved)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [residual] = f(N)
global Stoichiometry
global E
global T
global P_Po
%the element balance equations
residual(1:3) = Stoichiometry*N - E;
%the equilibrium constants
[LnKp1,LnKp2] = LnKp(T);
%equilibrium equations
residual(4) = -LnKp1 + log(P_Po/sum(N))+3*log(N(4)) + log(N(3))-2*log(N(5))-log(N(1));
residual(5) = - LnKp2 + log(N(5))+log(N(2))- log(N(3))-log(N(4));
residual = residual';
return



Many Thanks.

Subject: Not able to call mFile from command window.

From: Steven_Lord

Date: 18 Jun, 2012 13:35:10

Message: 2 of 2



"Zaki " <zaki.yz@gmail.com> wrote in message
news:jrmv8u$cgj$1@newscl01ah.mathworks.com...
> Hi
> I'm in the process of learning fsolve and jacobian. I read this mFile in a
> note and tried it out, wanting to learn from there. However, I cannot
> manage to call the mFile from the command window. I tried giving name to
> the Mfile as main.m and other tries as well, but still when I call the
> mFile, it won't run in the command window and will result to error. I also
> searched in the newsreader and google it but I cannot find how to call
> this mFile.

At first glance, this looks like a valid set of MATLAB function files.
Please show the group exactly how you tried to call the main function from
the Command Window and the FULL text of the error messages you received when
you did.

My guess is that you wrote:

    main.m

To call a function with no inputs, type its name WITHOUT the extension at
the prompt. You can use empty parentheses if you want, or you can omit them.

    main
    main()

*snip code*

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us