From: ahrid on 11 Aug 2010 06:03 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
From: machatsk on 11 Aug 2010 10:52 ahrid <samsunsporthebest(a)msn.com> What errors are you getting? What is your actual problem? What isn't working? Maxx
From: ahrid on 11 Aug 2010 07:20 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
From: machatsk on 11 Aug 2010 12:42 ahrid <samsunsporthebest(a)msn.com> wrote in message <1662394615.93764.1281540065549.JavaMail.root(a)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
From: Walter Roberson on 11 Aug 2010 14:12
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 . |