Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: MATLAB inconsistency
Date: Tue, 10 Aug 2010 03:10:06 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 76
Message-ID: <i3qfue$d4q$1@fred.mathworks.com>
References: <i3phfs$p51$1@fred.mathworks.com> <i3phok$nd$2@canopus.cc.umanitoba.ca> <i3pj80$hea$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1281409806 13466 172.30.248.37 (10 Aug 2010 03:10:06 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 10 Aug 2010 03:10:06 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: news.mathworks.com comp.soft-sys.matlab:660546

"Mohammad " <jaber2@uni.uiuc.edu> wrote in message <i3phfs$p51$1@fred.mathworks.com>...
> Dear reader,
> 
> I have a sinusoidal function, v(x), which should be odd, since sin is an odd function, and x is an odd function, so sin(...)-x should be odd.
> However when you plot it
> 
> Star=[0.592606202481246,-0.238596363969608,0.110220888524900,-0.048828233710702,0.019375152829649,-0.006571603729450,0.001814612417602,-3.812550640549345e-04,5.414560164816563e-05,-3.901808006180736e-06];
> v = @(x) sin(pi*x(:)*(1:r))*Star.'+a-x;
> fplot(v,[n o])
> 
> it is not odd. Why?
> Thanks!
> Cordially, Mohammad

"Mohammad " <jaber2@uni.uiuc.edu> wrote in message <i3pj80$hea$1@fred.mathworks.com>...
> z = 1;
> n = -.5;
> o = .5;
> a = 0;
> r = 10;
> b = 1;
> mat=zeros(r,r+1);
> for i= 1:r
>     for j = 1:r
>         mat(i,j) = sin(j*pi*(o-(o-n)*(i-1)/2/r));%(o-(o-n)*(i-1)/2/r)=x
>     end
>     for j = 1+r
>         mat(i,j) = z*((o-(o-n)*(i-1)/2/r))^b;
>     end
> end
> for j=2:r,
>      for i=j:r,
>         mat(i,:) = mat(i,:) - mat(j-1,:)*mat(i,j-1)/mat(j-1,j-1);
>      end
> end
> Bat = rot90(mat,2);
> Hat=[Bat Bat(:,1)];
> Hat(:,1)=[];
> for j=2:r,
>      for i=j:r,
>         Hat(i,:) = Hat(i,:) - Hat(j-1,:)*Hat(i,j-1)/Hat(j-1,j-1);
>      end
> end
> Fat = rot90(Hat,2);
> Rat=[Fat Fat(:,1)];
> Rat(:,1)=[];
> Star=zeros(1,r);
> for i = 1:r
>     Star(1,i)=Rat(i,end)/Rat(i,i);
> end
> v = @(x) sin(pi*x(:)*(1:r))*Star.'+a-x^b;
> fplot(v,[n o])
> fid = fopen('hope.txt', 'w');
> number3 = a;
> fprintf(fid,['A0 is ' num2str(number3)]);
> for i = 1:r
>     number = Star(1,i);
>     number2 = i;
>     fprintf(fid,['\nA' num2str(number2) ' is ' num2str(number)]);
> end
> fclose(fid);
> type hope.txt
- - - - - - - - - -
  Hello Mohammad.  I think I know what went wrong with fplot.  It apparently started from the 'n' end at x = -.5 and initially spaced x points close together.  However during the long stretch in the middle where your v values are very nearly a constant zero, it got overconfident and began to space the x values farther and farther apart until near the other 'o' end at x = +.5 the last five x values out of a total of 121 it tried were:

 x = -.208
 x = -.144
 x = -.016
 x = +.240
 x = +.500

This huge gap misses all the interesting variation in v at the right end and accounts for why the plot is so terribly inaccurate.  It is the kind of behavior that functions like quad do in their calls to integrand functions which remain nearly constant for too long an interval.

  My advice would be not to use fplot at all on the kind of stuff you're working with.  Just use the straight 'plot' function.  That way you know exactly what spacing is being used in the plotting.  It is not being left up to some algorithm which is trying to be too smart for its own good.

Roger Stafford