How to represent function which is defined on different interval?
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
I'm trying to plot function that defined on different intervals like following.
Let
phi_i(x)=M*(x-(i-1)/M) on [(i-1)/M,i/M] and M*((i+1)/M-x) on [i/M,(i+1)/M] and 0 on other values.
I got values u_1, u_2 ...u_(M-1) and note u(x)=u_1 phi_1(x)+u_2 phi_2(x) +...+u_(M-1) phi(x)
How to plot u(x)?
채택된 답변
Rik
2018년 5월 27일
You can use a for-loop to compose the function out of the parts using anonymous functions:
phi_part_i=@(x,i) ...
M*(x-(i-1)/M)*( (i-1)/M <x & x< i/M ) + ...
M*((i+1)/M-x)*( i/M <x & x< (i+1)/M );
phi_1=@(x) phi_part_i(x,1);
phi_1_10=@(x) 0;
for m=1:10
phi_1_10=@(x) phi_1_10(x) + phi_part_i(x,m);
end
댓글 수: 7
Jinman Park
2018년 5월 27일
편집: Jinman Park
2018년 5월 27일
Wow!! I've never seen code like this before!!
Thanks for your comment
Jinman Park
2018년 5월 28일
편집: Jinman Park
2018년 5월 28일
Can I ask you another question?
I wrote code
j=1;
g=@(x) 0;
phi_part_i=@(x,i) N*(x-(i-1)/N)*( ((i-1)/N <x) & (x<i/N) ) + N*((i+1)/N-x)*((i/N <x) & (x<(i+1)/N));
while(j<=(N-1))
g=@(x) g(x)+u(j,1)*phi_part_i(x,j);
j=j+1;
end
fplot(@(x) g(x),@(x) sin(pi*x),[0,1])
But, I got error
> In matlab.graphics.function.ParameterizedFunctionLine>getFunction
In matlab.graphics.function.ParameterizedFunctionLine/set.XFunction
In matlab.graphics.function.ParameterizedFunctionLine
In fplot>singleFplot (line 243)
In fplot>@(f1,f2)singleFplot(cax,{f1,f2},limits,extraOpts,args) (line 201)
In fplot>vectorizeFplot (line 201)
In fplot (line 163)
In week6_1_3 (line 27)
Can you give me an advice?
phi_part_i = @(x,i) N*(x-(i-1)/N).*( ((i-1)/N <x) & (x<i/N) ) + N*((i+1)/N-x).*((i/N <x) & (x<(i+1)/N));
Thanks.
Additionally, I found another mistake that I made.
I modified last part of my code to
fplot(g(x),@(x) sin(pi*x),[0,1])
Well, I'm really sorry to ask you another one.
I wrote similar code and I followed your advice.
j=1;
g=@(x) 0;
phi_odd_i=@(x,i)-4*(N^2)*((x-((i-1)/(2*N)))*(x-((i+1)/(2*N)))).*( ((i-1)/(2*N) <x) & (x<=(i+1)/(2*N)) );
phi_even_i=@(x,i) 2*(N^2)*((x-((i-2)/(2*N)))*(x-((i-1)/(2*N)))).*( ((i-2)/(2*N) <x) & (x<=i/(2*N)) ) + 2*(N^2)*((x-((i+2)/(2*N)))*(x-((i+1)/(2*N)))).*((i/(2*N) <x) & (x<=(i+2)/(2*N)));
while(j<=(2*N-1))
if(mod(j,2)==0)
g=@(x) g(x)+u(j,1)*phi_even_i(x,j);
else
g=@(x) g(x)+u(j,1)*phi_odd_i(x,j);
end
j=j+1;
end
fplot(g,[0,1],'b')
hold on
fplot(@(x) sin(pi*x),[0,1],'r')
hold off
end
But, I face the same error. Can you give me one more advice?
Notice how I change the * operators into .* operators in my response to you before. You need to do the same thing.
Thanks.
Your advice worked!!
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Time Series Events에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
