Plotting values from a function in different script

조회 수: 7 (최근 30일)
Sarah Gomez
Sarah Gomez 2022년 2월 11일
편집: Sarah Gomez 2022년 2월 11일
function [xvals,yvals]=fourier(L,nmax,numpoints)
xvals=[];
yvals=[];
for x=1:numpoints
y=0;
for n=1:2:nmax
y=y+(1/n)*(sin((n*pi*x)/L));
end
y=4*y/pi;
xvals=[xvals;x];
yvals=[yvals;y];
end
end
====================================================
[xvals,yvals]=fourier(L,nmax,numpoints);
x = xvals;
y = yvals;
L= 0;
plot(x, y);
I have a function that returns two very long column vectors for xvals and yvals. I'm struggling to create another script that calls upon this function and plots the respective xvals vs yvals. The code below the line gives me an error saying L is not recognized but I don't use it so I'm a bit confused.

채택된 답변

KSSV
KSSV 2022년 2월 11일
편집: KSSV 2022년 2월 11일
You have to define the required input variables first and then call the function.
L = 1. ;
nmax = 100 ;
numpoints = 1000 ;
[x,y]=fourier(L,nmax,numpoints);
plot(x,y);
function [xvals,yvals]=fourier(L,nmax,numpoints)
xvals=zeros(numpoints,1) ;
yvals=zeros(numpoints,1) ;
for i=1:numpoints
y=0;
for n=1:2:nmax
y=y+(1/n)*(sin((n*pi*i)/L));
end
y=4*y/pi;
yvals(i) = y ;
xvals(i) = i ;
end
end
  댓글 수: 3
KSSV
KSSV 2022년 2월 11일
편집: KSSV 2022년 2월 11일
It depends on the input you have given. Show us your input.
You might be inputting L as zero? Is it... If so change L, L cannot be zero, As it comes in the denominator, all your y values will be zero.
I have edited the code.
Sarah Gomez
Sarah Gomez 2022년 2월 11일
편집: Sarah Gomez 2022년 2월 11일
I've tried returning a sign wave for random inputs but I can't get it right.
[xvals, yvals] = fourier(0.1, 3, 200)
[xvals, yvals] = fourier(0.1, 9, 200)
[xvals, yvals] = fourier(0.1, 100, 200)
They all return fine but not in the shape I want.
For example the top tests returns a graph like this

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Descriptive Statistics and Visualization에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by