How to represent function which is defined on different interval?
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
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)?
Accepted Answer
Rik
on 27 May 2018
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 Comments
Jinman Park
on 27 May 2018
Edited: Jinman Park
on 27 May 2018
Wow!! I've never seen code like this before!!
Thanks for your comment
Jinman Park
on 28 May 2018
Edited: Jinman Park
on 28 May 2018
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?
Walter Roberson
on 28 May 2018
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));
Jinman Park
on 28 May 2018
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])
Jinman Park
on 28 May 2018
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?
Walter Roberson
on 28 May 2018
Notice how I change the * operators into .* operators in my response to you before. You need to do the same thing.
Jinman Park
on 29 May 2018
Thanks.
Your advice worked!!
More Answers (0)
Categories
Find more on Time Series Events in Help Center and File Exchange
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)