plotting and for loop
2 views (last 30 days)
Show older comments
I'm plotting this function and the plotted line is blocky not smooth line. i want the line connecting the four input points become smoothy. without using the all point between them. Points are (0.20,0.23,0.25,0.27) linspace( x,y ) command generate all points and connected it , I'm trying using For loop to generate the plotting but it doesn't workout!!! any suggested command
the function
function F =P
P= [0.20,0.23,0.25,0.27];
e1=(1+1.8*10^14*1i);
e2=(2.5+2.5*10^-3*1i);
Hc=(10^6);
H1=(0);
Pc=(0.33).*exp((-abs(H1)/Hc));
c1=(1-3*Pc).*(((P)./(Pc)).^Pc).*(((1-P)./(1-Pc)).^(1-Pc));
F=(1./(2.*(2+c1))).*(((3*P-1+c1)*e1+(2-3.*P-c1)*e2)+(((((3*P-1+c1)*e1+(2-3.*P)*e2).^2)+(4.*(e1).*(e2).*(1-c1).*(2+c1))).^0.5));
plot(P,real(F))
title('Loss'),xlabel('(P)pressure '),ylabel('Force')
legend('<H>=0')
end
2 Comments
Image Analyst
on 13 Jun 2019
Why are you assigning P ( the function name) to a 4 element vector inside the function itself? Is that even allowed? You should pick different names for the function and the variable.
Answers (2)
Walter Roberson
on 12 Jun 2019
You can generate a smooth curve between four points by doing interpolation, such as spline or pchip or makima ,
However, looking at your expression it is obvious that any attempt to do that would not give good results.
function F =P
P0= [0.20,0.23,0.25,0.27];
P = linspace(0.20, 0.27);
e1=(1+1.8*10^14*1i);
e2=(2.5+2.5*10^-3*1i);
Hc=(10^6);
H1=(0);
Pc=(0.33).*exp((-abs(H1)/Hc));
F = calc_f(P, Pc, e1, e2);
F0 = calc_f(P0, Pc, e1, e2);
Fint = interp1(P0, F0, P, 'spline');
subplot(1,2,1)
plot(P, real(F), 'k', P0, real(F0), 'b', P, real(Fint), 'r--')
title('Loss'),xlabel('(P)pressure '),ylabel('Force')
legend({'continuous', 'pointwise', 'interpolated'})
subplot(1,2,2)
plot(P, real(F)-real(Fint))
legend('continuous minus interpolated')
end
function F = calc_f(P, Pc, e1, e2);
c1=(1-3*Pc).*(((P)./(Pc)).^Pc).*(((1-P)./(1-Pc)).^(1-Pc));
F=(1./(2.*(2+c1))).*(((3*P-1+c1)*e1+(2-3.*P-c1)*e2)+(((((3*P-1+c1)*e1+(2-3.*P)*e2).^2)+(4.*(e1).*(e2).*(1-c1).*(2+c1))).^0.5));
end
0 Comments
Ammar Ahmed
on 13 Jun 2019
2 Comments
Walter Roberson
on 13 Jun 2019
You missed the point. I plot three different ways: your original four points, a fine grid, and spline interpolation. Then I plot the differences between the fine grid and the interpolation and show that interpolation does not do a good job.
MATLAB does not have a graphics primitive to input a few points and have it drawn a smooth curve. Any routine for that task would need to use interpretation. Which I show is not all that accurate.
See Also
Categories
Find more on Smoothing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!