Plot integral of a function

조회 수: 2 (최근 30일)
Elinor Ginzburg
Elinor Ginzburg 2020년 12월 27일
댓글: Star Strider 2020년 12월 28일
Hello,
I'm tryign to plot the integral of the function rho in the following code, but I'm having trouble with plotting it. Perhaps anyone can help me with the code for plotting the integral of this rho function in the different areas? This is what I've tried:
q = 1.5*10^-16;
a = 1;
Na = 10^16;
xn = 3;
xp = -2;
rho_righ = @(x) a.*x;
rho_left = @(x) -q*Na;
nop = @(x) 0;
figure(1);
fplot(rho_righ, [0,xn]);
hold on;
fplot(rho_left, [xp,0]);
hold on;
fplot(nop, [-5,xp]);
hold on;
fplot(nop, [xn,5]);
grid on;
legend('{\rho}=qax','{\rho}=-qNa');
hold off;
upper_limit = linspace(-xp, 0);
upper_limit2 = linspace(0, xn);
xval = arrayfun(@(uplim) integral(rho, 0, uplim, 'ArrayValued',true), upper_limit);
xval2 = arrayfun(@(uplim) integral(rho, 0, uplim, 'ArrayValued',true), upper_limit2);
figure(2)
plot(upper_limit, xval);
hold on;
plot(upper_limit2, xval2);
grid on;
Thank you very much!

채택된 답변

Star Strider
Star Strider 2020년 12월 27일
The problem appears to be that ‘rho’ itself does not exist.
I have no idea what you want ‘rho’ to be, however defining it as:
rho = @(x) rho_left(x) .* (x <= 0) + rho_righ(x) .* (x > 0);
will likely be an appropriate place to begin, and that produces figure(2) (and myriad Warning messages).. Whatever you intend it to be, it needs to be defined.
  댓글 수: 4
Elinor Ginzburg
Elinor Ginzburg 2020년 12월 28일
Thank you so much!
yeah that was exactly what I ment, and the results match my calculations.
Thank you very much for your help and advices!
Star Strider
Star Strider 2020년 12월 28일
As always, my pleasure!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by