Trying a overlapping but piecewise plot
显示 更早的评论
Here is my code:
clear all
close all
clc
syms a(x)
N1_0 = - (3*x^5)/16 + (5*x^3)/8 - (15*x)/16 + (1/2);
N2_0 = (3*x^5)/16 - (5*x^3)/8 + (15*x)/16 + 1/2;
N1_1 = - (3*x^5)/16 + x^4/16 + (5*x^3)/8 - (3*x^2)/8 - (7*x)/16 + 5/16;
N2_1 = - (3*x^5)/16 - x^4/16 + (5*x^3)/8 + (3*x^2)/8 - (7*x)/16 - 5/16;
N1_2 = - x^5/16 + x^4/16 + x^3/8 - x^2/8 - x/16 + 1/16;
N2_2 = x^5/16 + x^4/16 - x^3/8 - x^2/8 + x/16 + 1/16;
x1 = 0;
x2 = 0.25;
x3 = 0.5;
x4 = 0.75;
x5 = 1;
a(x) = piecewise(x1<=x<=x2,0,x4<=x<=x5,0,x3<=x<=x4,N1_0,x2<=x<=x3,N2_0,x2<=x<=x3,N1_1,x3<=x<=x4,N1_1,x2<=x<=x3,N2_0,x2<=x<=x3,N2_1,x3<=x<=x4,N2_2)
fplot(a)
The idea is to overlap N1_0 to N2_2 on to 0.5, without a residual solution, but the functions should overlap each other on the same plot/line
This is the pencil sketch of how it should look like:

Feel free to ask if you want to know anything about this
Using piecewise function was my idea (coz I thought it would work but it didn't) but it is not necessary.
Feel free to modify the code as you please
Using MATLAB online
Please help
Thanks
采纳的回答
The problem description leaves much to the imagination. If you want to offset them on the x-axis, this likely requires a loop. (I also made them functions in the event that the function argument was to be shifted, so for example the first would be ‘N1_0(x)’ the second ‘N1_0(x+0.5)’ and so for the others.) Here, I just shifted the fplot evaluation intervals.
syms a(x)
N1_0(x) = - (3*x^5)/16 + (5*x^3)/8 - (15*x)/16 + (1/2);
N2_0(x) = (3*x^5)/16 - (5*x^3)/8 + (15*x)/16 + 1/2;
N1_1(x) = - (3*x^5)/16 + x^4/16 + (5*x^3)/8 - (3*x^2)/8 - (7*x)/16 + 5/16;
N2_1(x) = - (3*x^5)/16 - x^4/16 + (5*x^3)/8 + (3*x^2)/8 - (7*x)/16 - 5/16;
N1_2(x) = - x^5/16 + x^4/16 + x^3/8 - x^2/8 - x/16 + 1/16;
N2_2(x) = x^5/16 + x^4/16 - x^3/8 - x^2/8 + x/16 + 1/16;
Nc = {N2_0; N1_1; N2_1; N1_2; N2_2};
x1 = 0;
x2 = 0.25;
x3 = 0.5;
x4 = 0.75;
x5 = 1;
% a(x) = piecewise(x1<=x<=x2,0,x4<=x<=x5,0,x3<=x<=x4,N1_0,x2<=x<=x3,N2_0,x2<=x<=x3,N1_1,x3<=x<=x4,N1_1,x2<=x<=x3,N2_0,x2<=x<=x3,N2_1,x3<=x<=x4,N2_2)
% fplot(a)
xv = [0 5];
figure
fplot(N1_0,xv)
hold on
for k = 1:numel(Nc)
fplot(Nc{k},[xv(1)+0.5*k xv(2)])
end
hold off
grid

The piecewise approach would work for discontinuous funcitons (so each defined over different, consecutive intervals of ‘x’), however that is not my impression of what you want to do.
.
8 个评论
Thanks for the help.
Your concept is cool but it doesn't achieve the "sketched" output I attached in the post.
Basically, each function that is orignially within the range [-1 1] is being compressed to 0.25 with a horizontal shift. The shape of the function in [-1 1] should remain the same but its length should now be 0.25 and each function has its own specified region. The final output is between x = 0 to x=1, just as I have shown in my sketch
Thanks again
‘Your concept is cool but it doesn't achieve the "sketched" output I attached in the post.’
I don’t understand the ‘sketched output’ because it bears no resemblance to any of the actual functions.
‘Basically, each function that is orignially within the range [-1 1] is being compressed to 0.25 with a horizontal shift.’
This makes no sense to me.
.
lets say the function N1_0, it is originally being considered only in the range x=-1 and x=+1. This shape should not change in the final output.
That is what I have shown in the sketch, if you look carefully makred as N(subscript1)(superscript0)
but what is different for the output is that with the same shape in mind (the height can change but not the shape it gives), it should compress to a range of x = 0 to x = 0.25 but shifted from x = 0.5 to 0.75 (as shown in sketch).
Something like this:

It took a few minutes for me to figure out the best way to do this.
Still using fplot, this appears to work —
syms a(x)
N1_0(x) = - (3*x^5)/16 + (5*x^3)/8 - (15*x)/16 + (1/2);
N2_0(x) = (3*x^5)/16 - (5*x^3)/8 + (15*x)/16 + 1/2;
N1_1(x) = - (3*x^5)/16 + x^4/16 + (5*x^3)/8 - (3*x^2)/8 - (7*x)/16 + 5/16;
N2_1(x) = - (3*x^5)/16 - x^4/16 + (5*x^3)/8 + (3*x^2)/8 - (7*x)/16 - 5/16;
N1_2(x) = - x^5/16 + x^4/16 + x^3/8 - x^2/8 - x/16 + 1/16;
N2_2(x) = x^5/16 + x^4/16 - x^3/8 - x^2/8 + x/16 + 1/16;
Nc = {N1_0; N2_0; N1_1; N2_1; N1_2; N2_2};
x1 = 0;
x2 = 0.25;
x3 = 0.5;
x4 = 0.75;
x5 = 1;
% a(x) = piecewise(x1<=x<=x2,0,x4<=x<=x5,0,x3<=x<=x4,N1_0,x2<=x<=x3,N2_0,x2<=x<=x3,N1_1,x3<=x<=x4,N1_1,x2<=x<=x3,N2_0,x2<=x<=x3,N2_1,x3<=x<=x4,N2_2)
% fplot(a)
lgdc = {'N_1^0','N_2^0','N_1^1','N_2^1','N_1^2','N_2^2'};
xv = [-1 1];
figure
hold on
for k = 1:numel(Nc)
hfp{k} = fplot(Nc{k},xv, 'DisplayName',lgdc{k});
end
hold off
grid
title('Original')
legend('Location','NE')

xv = [-1 1];
figure
hold on
for k = 1:numel(Nc)
hfp{k} = fplot(Nc{k},xv);
hfp{k}.Visible = 'off';
x = hfp{k}.XData;
y = hfp{k}.YData;
hp{k} = plot(x*0.125 + 0.125 + 0.5*k, y , 'DisplayName',lgdc{k});
end
hold off
grid
xlim([0 max(xlim)])
title('X-Scaled & X-Shifted')
legend([hp{:}],'Location','best')

I am not certain that I understood exactly the result you want, so make appropriate changes to get it.
The ‘x’ calculation is:
x*0.125 + 0.125 + 0.5*k
↑← SF ↑← CO ↑← VO
where ‘SF’ is the scaling factor to get the ‘x’ values to span 0.25, ‘CO’ is a constant offset, and ‘VO’ is the variable offset that increments with the loop. Change those as necessary to get the desired result.
.
Thank you so much All I have to do is make some minor adjustments to match my first sketch but this is more than enough. Thanks a lot
As always, my pleasure!
check this out, gives the exact first sketch output.
figure
hold on
for k = [1 4 6]
hfp{k} = fplot(Nc{k},xv);
hfp{k}.Visible = 'off';
x = hfp{k}.XData;
y = hfp{k}.YData;
hp{k} = plot(x*0.125 +.625, y , 'DisplayName',lgdc{k});
end
for k = [2 3 5]
hfp{k} = fplot(Nc{k},xv);
hfp{k}.Visible = 'off';
x = hfp{k}.XData;
y = hfp{k}.YData;
hp{k} = plot(x*0.125 +.375, y , 'DisplayName',lgdc{k});
end
hold off
grid
xlim([0 1])
title('X-Scaled & X-Shifted')
legend([hp{:}],'Location','best')
Still, all credit goes to you
PS I dont know how you run it here with the graphical and numerical output but still thanks a lot for all the help
As always, my pleasure!
Thank you!
I was curious, so I ran it to see what it looked like —
syms a(x)
N1_0(x) = - (3*x^5)/16 + (5*x^3)/8 - (15*x)/16 + (1/2);
N2_0(x) = (3*x^5)/16 - (5*x^3)/8 + (15*x)/16 + 1/2;
N1_1(x) = - (3*x^5)/16 + x^4/16 + (5*x^3)/8 - (3*x^2)/8 - (7*x)/16 + 5/16;
N2_1(x) = - (3*x^5)/16 - x^4/16 + (5*x^3)/8 + (3*x^2)/8 - (7*x)/16 - 5/16;
N1_2(x) = - x^5/16 + x^4/16 + x^3/8 - x^2/8 - x/16 + 1/16;
N2_2(x) = x^5/16 + x^4/16 - x^3/8 - x^2/8 + x/16 + 1/16;
Nc = {N1_0; N2_0; N1_1; N2_1; N1_2; N2_2};
x1 = 0;
x2 = 0.25;
x3 = 0.5;
x4 = 0.75;
x5 = 1;
lgdc = {'N_1^0','N_2^0','N_1^1','N_2^1','N_1^2','N_2^2'};
xv = [-1 1];
figure
hold on
for k = [1 4 6]
hfp{k} = fplot(Nc{k},xv);
hfp{k}.Visible = 'off';
x = hfp{k}.XData;
y = hfp{k}.YData;
hp{k} = plot(x*0.125 +.625, y , 'DisplayName',lgdc{k});
end
for k = [2 3 5]
hfp{k} = fplot(Nc{k},xv);
hfp{k}.Visible = 'off';
x = hfp{k}.XData;
y = hfp{k}.YData;
hp{k} = plot(x*0.125 +.375, y , 'DisplayName',lgdc{k});
end
hold off
grid
xlim([0 1])
title('X-Scaled & X-Shifted')
legend([hp{:}],'Location','best')

.
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Linear Algebra 的更多信息
另请参阅
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)
