DSPRelated.com
Forums

Matlab question

Started by kal July 26, 2006
Hello All,
In Matlab, I have this function:
y = 20 * (x.^2).* ((1-x).^4) .* cos (12 * pi * x);
When I try to see the result of y(0.5) on the m-file, the system
generates a synatx error:

??? Error using ==> run
Attempted to access y(0.5); index must be a positive integer or
logical.
The question is that how can I access this value as I need to sample
this function into 1000 intervals, and use these samples for discrete
analysis.
My program is:

function wave=haarX();
x = [0:0.001:1];


y = 20 * (x.^2).* ((1-x).^4) .* cos (12 * pi * x);
%y(0.5);
for i=1:length(x)
    Array(i) = y(i/1000);
end
Thanks for time and your response is much appreciated.
Sincerely,
Kal

Hi,

your 'y' in matlab is a matrix (1x1000). You can only get y(1),y(2)...

So if you want to get y(0.5) you need to find which index is 0.5, ie
n=3D0.5/0.001;
then
y(n)...

I hope my explanation is clear...

Regards,


kal a =E9crit :

> Hello All, > In Matlab, I have this function: > y =3D 20 * (x.^2).* ((1-x).^4) .* cos (12 * pi * x); > When I try to see the result of y(0.5) on the m-file, the system > generates a synatx error: > > ??? Error using =3D=3D> run > Attempted to access y(0.5); index must be a positive integer or > logical. > The question is that how can I access this value as I need to sample > this function into 1000 intervals, and use these samples for discrete > analysis. > My program is: > > function wave=3DhaarX(); > x =3D [0:0.001:1]; > > > y =3D 20 * (x.^2).* ((1-x).^4) .* cos (12 * pi * x); > %y(0.5); > for i=3D1:length(x) > Array(i) =3D y(i/1000); > end > Thanks for time and your response is much appreciated. > Sincerely, > Kal
kal wrote:
> Hello All, > In Matlab, I have this function: > y = 20 * (x.^2).* ((1-x).^4) .* cos (12 * pi * x); > When I try to see the result of y(0.5) on the m-file, the system > generates a synatx error: > > ??? Error using ==> run > Attempted to access y(0.5); index must be a positive integer or > logical. > The question is that how can I access this value as I need to sample > this function into 1000 intervals, and use these samples for discrete > analysis. > My program is: > > function wave=haarX(); > x = [0:0.001:1]; > > > y = 20 * (x.^2).* ((1-x).^4) .* cos (12 * pi * x); > %y(0.5); > for i=1:length(x) > Array(i) = y(i/1000); > end > Thanks for time and your response is much appreciated. > Sincerely, > Kal
Aren't you trying to do: x = [0:0.0001:1]; for i = 1:length(x) Array(i) = 20 * (x.^2).*((1-x).^4)*cos(12*pi*x) / 1000; end %Array(5000); or is that %Array[5000]; ??? Of course you can't take y(0.5) as Matlab states very simply that the index needs to be integer. The half way sample point of your 10, 000x samples (yup that's 10, 000 otherwise use x=[0:0.001:1]) is the 5000 value.
kal skrev:
> Hello All, > In Matlab, I have this function: > y = 20 * (x.^2).* ((1-x).^4) .* cos (12 * pi * x); > When I try to see the result of y(0.5) on the m-file, the system > generates a synatx error: > > ??? Error using ==> run > Attempted to access y(0.5); index must be a positive integer or > logical. > The question is that how can I access this value as I need to sample > this function into 1000 intervals, and use these samples for discrete > analysis. > My program is: > > function wave=haarX(); > x = [0:0.001:1]; > > > y = 20 * (x.^2).* ((1-x).^4) .* cos (12 * pi * x); > %y(0.5); > for i=1:length(x) > Array(i) = y(i/1000); > end > Thanks for time and your response is much appreciated. > Sincerely, > Kal
yidx=find(x==0.5); Y = y(yidx); Rune
kal wrote:
> Hello All, > In Matlab, I have this function: > y = 20 * (x.^2).* ((1-x).^4) .* cos (12 * pi * x); > When I try to see the result of y(0.5) on the m-file, the system > generates a synatx error: > > ??? Error using ==> run > Attempted to access y(0.5); index must be a positive integer or > logical. > The question is that how can I access this value as I need to sample > this function into 1000 intervals, and use these samples for discrete > analysis. > My program is: > > function wave=haarX(); > x = [0:0.001:1]; > > > y = 20 * (x.^2).* ((1-x).^4) .* cos (12 * pi * x); > %y(0.5); > for i=1:length(x) > Array(i) = y(i/1000); > end
Kal -- in your program, you DO NOT have a function y(x); you have a MATLAB statement creating a vector y from another vector x. If you wish to use y(x) as a function, then I suggest that your program should look something like this, with an internal function y(x): function wave=haarX(); x = [0:0.001:1]; for i=1:length(x) Array(i) = y(i/1000); end end function value = y(x) value = 20 * (x.^2).* ((1-x).^4) .* cos (12 * pi * x); end cheers, jerry