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:
help on molar volume using global function and newton iteration

Subject: help on molar volume using global function and newton iteration

From: ahrid

Date: 11 Aug, 2010 14:03:26

Message: 1 of 6

hy,

I am quite new here and I don't have much experience regarding programming and Matlab. But i had an assingment to make a file that calculates the molar volume v.

I tried lots of things and the best code i have till now is the next:

function v=mainfunction(P,T)
global P0
global T0
    P0=P;
    T0=T;
    a=0; b=100;
    v0= ((8.31*T)/P);
   format long
   v=newton('fv','dfv',v0,a,b,10^(-10),1000)
  end
and this is my secondary function called fv and dfv where I have to use global function:

function y=fv
global P0
global T0

v=((8.31*T0)/P0);
y=(P0+(4.16/(v^2)))*(v-0.06)-(8.31*T0);

end

and

function y=dfv
global P0
global T0
v=((8.31*T0)/P0);
y= P0 - (4.16/(v^2))+((2*4.16*0.05)/(v^3));
end

this secondary functions I have to use in my function called newton, that uses the newton iteration:

function x= newton(fun, dfun,x0,a,b,tol,nmax)
k=0;
while ((k< nmax)&&((abs(fun(x0)))> tol));
       x= x0- (feval(fun,x0)/feval(dfun,x0));
       k= k+1;
if k > nmax
    error(' numbers of iterations above Nmax')
end
if ((x0< (a:b)) | (x0>(a:b)));
    error('the iteration is outside interval');
end
end
end

I assume my code for the newton iteration is right, but I have trouble using the global function.

Now my question is if anyone could tell where im doing wrong ?

thanks in advance,
ahrid

Subject: help on molar volume using global function and newton iteration

From: machatsk

Date: 11 Aug, 2010 14:52:05

Message: 2 of 6

ahrid <samsunsporthebest@msn.com>

    What errors are you getting? What is your actual problem? What isn't working?
                        Maxx

Subject: help on molar volume using global function and newton iteration

From: ahrid

Date: 11 Aug, 2010 15:20:35

Message: 3 of 6

srry forgot to mention that

i get the following error:
??? Subscript indices must either be real positive integers or logicals.

Error in ==> newton at 3
while ((k< nmax)&&((abs(fun(x0)))> tol));

Error in ==> opdracht2 at 9
   v=newton('fv','dfv',v0,a,b,10^(-10),1000)
 

but when i try the function newton alone it will work for ex: newton('sin','cos',3,2,4,10^(-10),10) it give 3.14... as it should be

Subject: help on molar volume using global function and newton iteration

From: machatsk

Date: 11 Aug, 2010 16:42:05

Message: 4 of 6

ahrid <samsunsporthebest@msn.com> wrote in message <1662394615.93764.1281540065549.JavaMail.root@gallium.mathforum.org>...
> srry forgot to mention that
>
> i get the following error:
> ??? Subscript indices must either be real positive integers or logicals.
>
> Error in ==> newton at 3
> while ((k< nmax)&&((abs(fun(x0)))> tol));
>
> Error in ==> opdracht2 at 9
> v=newton('fv','dfv',v0,a,b,10^(-10),1000)
>
>
> but when i try the function newton alone it will work for ex: newton('sin','cos',3,2,4,10^(-10),10) it give 3.14... as it should be


      It looks like some of your variables may be in the wrong class. Try converting them to doubles (str2double) if they aren't. The inputs at line 3 for newton are the problem. Put a break in the code on that line and run the program (click on the "-" next to the line number (3) on the left of the editor). Then look through your variables and see if you can find a problem. Its probably pretty simple, since MATLAB has a way of doing that...
          Maxx

Subject: help on molar volume using global function and newton iteration

From: Walter Roberson

Date: 11 Aug, 2010 18:12:26

Message: 5 of 6

ahrid wrote:
> srry forgot to mention that
>
> i get the following error:
> ??? Subscript indices must either be real positive integers or logicals.
>
> Error in ==> newton at 3
> while ((k< nmax)&&((abs(fun(x0)))> tol));

Sounds like you somehow created a variable named abs or a variable named fun .

Subject: help on molar volume using global function and newton iteration

From: ahrid

Date: 11 Aug, 2010 20:20:41

Message: 6 of 6

> ahrid <samsunsporthebest@msn.com> wrote in message
> <1662394615.93764.1281540065549.JavaMail.root@gallium.
> mathforum.org>...
> > srry forgot to mention that
> >
> > i get the following error:
> > ??? Subscript indices must either be real positive
> integers or logicals.
> >
> > Error in ==> newton at 3
> > while ((k< nmax)&&((abs(fun(x0)))> tol));
> >
> > Error in ==> opdracht2 at 9
> > v=newton('fv','dfv',v0,a,b,10^(-10),1000)
> >
> >
> > but when i try the function newton alone it will
> work for ex: newton('sin','cos',3,2,4,10^(-10),10) it
> give 3.14... as it should be
>
>
> It looks like some of your variables may be in
> be in the wrong class. Try converting them to doubles
> (str2double) if they aren't. The inputs at line 3 for
> newton are the problem. Put a break in the code on
> that line and run the program (click on the "-" next
> to the line number (3) on the left of the editor).
> Then look through your variables and see if you can
> find a problem. Its probably pretty simple, since
> MATLAB has a way of doing that...
> Maxx

First of al thnx for ur reply
But what do u mean by converting my variables to doubles? I don't understand it verry well

thnx in advance

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