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)?
采纳的回答
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 个评论
Wow!! I've never seen code like this before!!
Thanks for your comment
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)
