Prev: edge linking problem
Next: Numerical Newton Rapshon
From: Walter Roberson on 8 Apr 2010 18:36 Muhamad wrote: > %func_prime.m > > function [value] = func_prime(x) > value = sin(A)-(sin(teta)*e^(-(180+A-teta)/WRC); > > x = 1; > > for j = 1:10 > x = x - func(x)/func_prime(x); > end When you have a function, the only values that are returned to the calling routine are the ones named before the '=' on the 'function' line. In this code, 'value' is the only variable named there. Once you have computed value, there is no point in doing any further work (unless it has to with graphics or outputs to a file or the display), because whatever you do will just be thrown away. Thus the 'for' loop is not contributing anything to this routine and can be discarded, just as the assignment to 'x' can be. When you have a function, the only values that you can use in the function are numeric constants, named constants such as pi, values you have already computed in the routine, and values that you have named after the '(' on the 'function' line. [I'm simplifying slightly; there are some more advanced facilities.] You name x in that position, but you do not use x. You do, though, attempt to use A, teta, and WRC, none of which fall into the categories I describe. Thus A, teta, and WRC are undefined as far as this routine is concerned. If you want to use the values that you input from the user, then you need to give names for them on the 'function' line, and you need to pass the values in when you call the routine. For example, you could call func_prime(A, WRC, teta) and your corresponding 'function' line would be function value = func_prime(A, WRC, teta)
From: Muhamad on 8 Apr 2010 19:03 Walter Roberson <roberson(a)hushmail.com> wrote in message <hpllok$hac$1(a)canopus.cc.umanitoba.ca>... > Muhamad wrote: > > > %func_prime.m > > > > function [value] = func_prime(x) > > value = sin(A)-(sin(teta)*e^(-(180+A-teta)/WRC); > > > > x = 1; > > > > for j = 1:10 > > x = x - func(x)/func_prime(x); > > end > > When you have a function, the only values that are returned to the calling > routine are the ones named before the '=' on the 'function' line. In this > code, 'value' is the only variable named there. Once you have computed value, > there is no point in doing any further work (unless it has to with graphics or > outputs to a file or the display), because whatever you do will just be > thrown away. Thus the 'for' loop is not contributing anything to this routine > and can be discarded, just as the assignment to 'x' can be. > > When you have a function, the only values that you can use in the function are > numeric constants, named constants such as pi, values you have already > computed in the routine, and values that you have named after the '(' on the > 'function' line. [I'm simplifying slightly; there are some more advanced > facilities.] You name x in that position, but you do not use x. You do, > though, attempt to use A, teta, and WRC, none of which fall into the > categories I describe. Thus A, teta, and WRC are undefined as far as this > routine is concerned. If you want to use the values that you input from the > user, then you need to give names for them on the 'function' line, and you > need to pass the values in when you call the routine. For example, you could call > > func_prime(A, WRC, teta) > > and your corresponding 'function' line would be > > function value = func_prime(A, WRC, teta) why but still error ? ------ ??? Input argument 'teta' is undefined. Error in ==> C:\Documents and Settings\madz\My Documents\Downloads\elda\func.m On line 4 ==> f = cos(A)-sin(teta)*e^(-(180+A-teta)/W*R); Error in ==> C:\Documents and Settings\madz\My Documents\Downloads\elda\Newton_Raphson.m On line 11 ==> while (iterations<30) & (abs(func(x))>tolerance) ----
From: Walter Roberson on 8 Apr 2010 19:21
Muhamad wrote: > Walter Roberson <roberson(a)hushmail.com> wrote in message > <hpllok$hac$1(a)canopus.cc.umanitoba.ca>... >> When you have a function, the only values that you can use in the >> function are numeric constants, named constants such as pi, values you >> have already computed in the routine, and values that you have named >> after the '(' on the 'function' line. > why but still error ? > ------ > ??? Input argument 'teta' is undefined. > > Error in ==> C:\Documents and Settings\madz\My > Documents\Downloads\elda\func.m > On line 4 ==> f = cos(A)-sin(teta)*e^(-(180+A-teta)/W*R); > > Error in ==> C:\Documents and Settings\madz\My > Documents\Downloads\elda\Newton_Raphson.m > On line 11 ==> while (iterations<30) & (abs(func(x))>tolerance) If we look at that last error message, we see that you are calling func() passing in only x. Inside func, you attempt to use teta. Review the paragraph I have quoted above. Is teta a numeric literal, a named constant such as pi, a value already computed in func(), or a variable you have named after the '(' of the 'function' line of func()? If the latter, have you passed in a value for teta from the calling routine? I never claimed to have fixed all of the problems in your code: I showed you what was wrong with a particular routine and why, and assumed you would be apply to take those explanations and apply the thought processes to your other routines and fix them yourself. |