Plot a matrix which has functions

I have these functions (in a script):
fx = @ (x) exp(-x.^2);
f1x = @ (x) 1/(x.^2+1);
fpx = f1x/int(f1x,x,0,1)
I want to plot a table with {fx,fpx} when {x,0,1}.
How can I do this?

 채택된 답변

Walter Roberson
Walter Roberson 2011년 1월 26일

0 개 추천

int() is a routine that applies to symbolic expressions, not to function handles. Do you have access to the Symbolic Toolkit? If not, then you will need to do a numeric integration such as with trapz or quadl
If you are using the symbolic toolbox, you would rewrite all three clauses. If you are not, then your last clause should not be
fpx=f1x/int(f1x,x,0,1)
but rather something like
fpx = @(x) f1x(x) ./ quadl(f1x,0,1)
It should not, however, be something like
fpx = @(x) double(subs(f1x,'x',x)) ./ double(int(f1x,x,0,1))
which is close but has the problem that the "x" in the call to "int" needs to be the symbolic variable to integrate with respect to, and should not be the particular value of x that you are using to evaluate f1x in the numerator. You could, however, use:
fpx = @(x) double(subs(f1x,'x',x)) ./ double(int(f1x,'x',0,1))
Once you have constructed fpx to be a function of x, you could construct your table as
SubDivisons = 250;
xvals = linspace(0,1,SubDivisions) .';
YourTable = [fx(xvals), fpx(xvals)];
plot(xvals, YourTable);

댓글 수: 4

George
George 2011년 1월 26일
Hello,i tried these but it gives me errors: Error using ==> mldivide
Matrix dimensions must agree.
Error in ==> @(x)1/(x.^2+1)
Error in ==> quadl at 70
y = feval(f,x,varargin{:}); y = y(:).';
Error in ==> @(x)f1x(x)./quadl(f1x,0,1)
Error in ==> sampling2 at 62
YourTable = [fx(xvals), fpx(xvals)];
I will check tomorrow again.Thanks for the help.
Walter Roberson
Walter Roberson 2011년 1월 26일
Change your @(x)1/(x.^2+1)
to
@(x)1./(x.^2+1)
George
George 2011년 1월 27일
It worked fine now!Thanks a lot!
George
George 2011년 1월 27일
May i ask sth else?
How can i write this : I want to integrate fp(z) with z(0,x).
I tried this :" fpz=@(z) f1x(z) ./ quadl(f1x,0,1)
int(fpz,0,x) "
but it doesn't work.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Paulo Silva
Paulo Silva 2011년 1월 26일

0 개 추천

disp('Executing the function, please wait')
steps=0.05;
%increase steps to speed up execution, accuracy will be lower
%decrease steps to improve accuracy, you have to wait more time
x=0:steps:1;
fx=inline('exp(-x.^2)');
f1x=inline('1./(x.^2+1)');
fpx=sym(f1x)./int(sym(f1x));
fpx=subs(fpx,'x',x);
fx=subs(fx,'x',x);
Table=[fx;fpx];
plot(fx,fpx)
disp('All done, you can find the result in variable Table')

댓글 수: 4

Ned Gulley
Ned Gulley 2011년 1월 26일
Hi Paulo: You might want to just edit your old answer to correct it rather than creating a new answer.
Paulo Silva
Paulo Silva 2011년 1월 26일
Hi Ned, I will use the edit next time, thanks.
George
George 2011년 1월 27일
Hello, i tried these but it gives me the following error:
Error using ==> sym.sym>tomupad at 2162
Conversion to 'sym' from 'inline' is not possible.
Error in ==> sym.sym>sym.sym at 102
S.s = tomupad(x,'');
Error in ==> test at 61
fpx=sym(f1x)./int(sym(f1x));
Paulo Silva
Paulo Silva 2011년 1월 27일
I have no idea about what could be wrong, it's working here in Matlab 2008b (32bits), here's the result http://img254.imageshack.us/img254/5666/46107520.jpg

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by