How do I plot points coming from a for loop without using vectors?

조회 수: 3 (최근 30일)
Hi, I am new to Matlab so excuse my ignorance. I am trying to make a code that evaluates a definite integral from 0 to infinity for different values of two parameters, which I called v,L in the script below. Then I want to plot these definite integrals versus values of, say, L, which increases by one in each cycle. Why aren't values of L on the horizontal axis equally spaced as they should be? Is there something wrong with the plot function? If so, is there a way to plot points coming from a for loop as they get out, without using vectors? Thanks in advance.
syms x;
double L;
double v;
f=L*exp(-v*x^2);
L=0;
v=1;
for i=0:50
L=L+i;
k=int(f,x,0,inf);
plot(L,k,'o');
hold on
end
hold off
  댓글 수: 3
Salvatore Manfredi D'Angelo
Salvatore Manfredi D'Angelo 2021년 7월 22일
Hi, thanks for your reply! I was actually testing out the code before letting double L take on decimal values on an interval
Konrad
Konrad 2021년 7월 22일
I think what Stephen refers to is that
double L; % = double('L')
doesn't declare a variable (as in other languages), but type-casts the character 'L' to type double, which is simply the number 76.

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

채택된 답변

Konrad
Konrad 2021년 7월 22일
Hi,
values on the x-axis are not equally spaced because on every interation you increase L by i, which itself is increased by 1 on every interation:
1st interation: L = 0+0 = 0
2nd L = 0+1 = 1
3rd L = 1+2 = 3
4th L = 3+3 = 6
...
but you can just use i as your x-parameter for the plot function:
plot(i,k,'o');
> "is there a way to plot points coming from a for loop as they get out, without using vectors?"
you allready do that using hold on
  댓글 수: 3
Konrad
Konrad 2021년 7월 22일
I'm not really familiar with the symbolic math toolbox and I don't know int() for integrals, sorry.
But here would be a numeric solution:
ffactory = @(L,v)@(x)L*exp(-v*x.^2); % function factory returning f as an anonymous function
v = 1;
for i = 0:50
f = ffactory(i,v);
k = integral(f,0,inf);
plot(i,k,'o');
hold on;
end
Salvatore Manfredi D'Angelo
Salvatore Manfredi D'Angelo 2021년 7월 22일
Thank you! This solved my issue. integral() is numeric and int() is symbolic then, I will keep in mind

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by