From: Peter on
New to Matlab, so a beginner question which after many hours I still can't find the answer to:

Defined a function, "planck":

=======
function i = planck( w,t )
% planck function for one value of wavelength, w and temp, t
% result in spectral intensity
i = 3.742E8.*(pi.*w.^5.*exp((1.439E4./(w.*t))-1)).^-1

end
=======
and it works fine when I call it from the command line, like planck(0.5,5780);

but I do notice - strange and does this mean a problem? - that the output is duplicated (and shouldn't the ";" suppress this anyway??):

"i =

7.1276e+007


ans =

7.1276e+007"

Anyway, then I defined a script to run and call this function. (Cut down version of the original idea here, because I kept "winding it back" to eliminate the error).

Script:
==
for w=.02:.02:100
y = planck(w,290);
end
====

But when I "play" the script the command line says:

=====
??? Input argument "w" is undefined.

Error in ==> planck at 4
i = 3.742E8.*(pi.*w.^5.*exp((1.439E4./(w.*t))-1)).^-1
=====

I hope someone can help, this is probably so easy, but I cannot find documentation with enough examples to supply the solution.

Thanks
From: dpb on
Peter wrote:
> New to Matlab, so a beginner question which after many hours I still
> can't find the answer to:
>
> Defined a function, "planck":
>
> =======
> function i = planck( w,t )
> % planck function for one value of wavelength, w and temp, t
> % result in spectral intensity
> i = 3.742E8.*(pi.*w.^5.*exp((1.439E4./(w.*t))-1)).^-1
>
> end
> =======
> and it works fine when I call it from the command line, like
> planck(0.5,5780);
>
> but I do notice - strange and does this mean a problem? - that the
> output is duplicated (and shouldn't the ";" suppress this anyway??):
>
> "i =
>
> 7.1276e+007
>
>
> ans =
>
> 7.1276e+007"

You don't have a semicolon inside the script; the first "i = " is coming
from that. Matlab echoes everything unless you suppress it (which is
handing for debugging, just leave the semicolon off a line to see it's
effect temporarily).

You also do _NOT_ want to write a function named "i" -- i (and j) are
defined functions in Matlab for the imaginary unit vectors and great
confusion arises from redefining them.



> Anyway, then I defined a script to run and call this function. (Cut down
> version of the original idea here, because I kept "winding it back" to
> eliminate the error).
>
> Script:
> ==
> for w=.02:.02:100
> y = planck(w,290);
> end
> ====
>
> But when I "play" the script the command line says:
>
> =====
> ??? Input argument "w" is undefined.
>
> Error in ==> planck at 4
> i = 3.742E8.*(pi.*w.^5.*exp((1.439E4./(w.*t))-1)).^-1
> =====
....

Hmmm....I don't see this otomh...

Think need to fix the previous comment re "i", add the semicolon in the
function definition, then do a

"clear all"

and try again. I'm thinking there's a problem w/ caching a previous
version or somesuch so that the function being evaluated isn't actually
the latest version you think is being called or a similar problem.

If that doesn't cure it; then repost w/ latest version and script in
context.

BTW, you recognize your script as given will overwrite the "y" each time
through the loop I presume?

--
From: Peter on
dpb <none(a)non.net> wrote in message <i2eqk5$1u4$1(a)news.eternal-september.org>...
> Peter wrote:
> > New to Matlab, so a beginner question which after many hours I still
> > can't find the answer to:
> >
> > Defined a function, "planck":
> >
> > =======
> > function i = planck( w,t )
> > % planck function for one value of wavelength, w and temp, t
> > % result in spectral intensity
> > i = 3.742E8.*(pi.*w.^5.*exp((1.439E4./(w.*t))-1)).^-1
> >
> > end
> > =======
> > and it works fine when I call it from the command line, like
> > planck(0.5,5780);
> >
> > but I do notice - strange and does this mean a problem? - that the
> > output is duplicated (and shouldn't the ";" suppress this anyway??):
> >
> > "i =
> >
> > 7.1276e+007
> >
> >
> > ans =
> >
> > 7.1276e+007"
>
> You don't have a semicolon inside the script; the first "i = " is coming
> from that. Matlab echoes everything unless you suppress it (which is
> handing for debugging, just leave the semicolon off a line to see it's
> effect temporarily).
>
> You also do _NOT_ want to write a function named "i" -- i (and j) are
> defined functions in Matlab for the imaginary unit vectors and great
> confusion arises from redefining them.
>

Ok - thanks I see your point and changed both of these - new function:
=====
function ri = planck( w,t )
% planck function for one value of wavelength, w and temp, t
% result in spectral intensity
ri = 3.742E8.*(pi.*w.^5.*exp((1.439E4./(w.*t))-1)).^-1;

end
=======

Once again it works fine, but now no echo.


>
>
> > Anyway, then I defined a script to run and call this function. (Cut down
> > version of the original idea here, because I kept "winding it back" to
> > eliminate the error).
> >
> > Script:
> > ==
> > for w=.02:.02:100
> > y = planck(w,290);
> > end
> > ====
> >
> > But when I "play" the script the command line says:
> >
> > =====
> > ??? Input argument "w" is undefined.
> >
> > Error in ==> planck at 4
> > i = 3.742E8.*(pi.*w.^5.*exp((1.439E4./(w.*t))-1)).^-1
> > =====
> ...
>
> Hmmm....I don't see this otomh...
>
> Think need to fix the previous comment re "i", add the semicolon in the
> function definition, then do a
>
> "clear all"
>
> and try again. I'm thinking there's a problem w/ caching a previous
> version or somesuch so that the function being evaluated isn't actually
> the latest version you think is being called or a similar problem.
>
> If that doesn't cure it; then repost w/ latest version and script in
> context.
>
> BTW, you recognize your script as given will overwrite the "y" each time
> through the loop I presume?
>
> --

I cleared all and ran my script (no change):
==
for w=.02:.02:100
y = planck(w,290);
end
====

But same error as before:

====
??? Input argument "w" is undefined.

Error in ==> planck at 4
ri = 3.742E8.*(pi.*w.^5.*exp((1.439E4./(w.*t))-1)).^-1;
=====

And what I was trying to do was create an array, y, the same size as w - not replace y with a new value each time through.
From: dpb on
Peter wrote:
....

> I cleared all and ran my script (no change):
> ==
> for w=.02:.02:100
> y = planck(w,290);
> end
> ====
>
> But same error as before:
>
> ====
> ??? Input argument "w" is undefined.
>
> Error in ==> planck at 4
> ri = 3.742E8.*(pi.*w.^5.*exp((1.439E4./(w.*t))-1)).^-1;
> =====

I built a function

function ri = planck( w,t )
% planck function for one value of wavelength, w and temp, t
% result in spectral intensity
ri = 3.742E8.*(pi.*w.^5.*exp((1.439E4./(w.*t))-1)).^-1;

and then,

>> for w=.02:.02:100,y = planck(w,290);end
>> y
y =
0.0197
>>

Works here. One thing I note is an unmatched "end" statement in your
function listing which makes me think there's something not being shown
here. As written otherwise should work afaict.

> And what I was trying to do was create an array, y, the same size as w -
> not replace y with a new value each time through.

As you wrote it, you don't need the loop for the one variable--that's
what the .*, ./ operators do is to evaluate on an element-by-element
basis to eliminate loops; Matlab's strength. Now if you want to do both
variables simultaneously, you'll need to build a grid or somesuch, but
as you've written it, planck() will return the vector directly.

>> y = planck([.2:.2:100], 290);
>> length(y)
ans =
500
>>

If you really do want to call it in a loop on a value-by-value basis
then that would look something like

y = zeros(length([.2:.2:100])); % preallocate
idx = 1;
for w=[.2:.2:100]
y(idx) = planck(w,290);
idx = idx+1;
end

--
From: Jan Simon on
Dear Peter,

the description of your Porblem sounds correct. The source code you've posted looks correct. The error message tells you, that it is not correct.

Scientific conclusion: You run other code than you have posted.

Ideas for a solution:
Do you have more than one function called planck?
which planck.m
Have you saved the file planck.m after editing?
Let the debugger show you, what's going on. Set a breakpoint in your function "planck" and run it again line by line.

Good luck, Jan
 |  Next  |  Last
Pages: 1 2 3
Prev: A possible bug in Windows Server 2008?
Next: reshape