plotting and for loop

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

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.
thanks for the comment , it was mistakenly assigned.
how about my question it is possible to have smooth line plot using for loop command !! could please help me with your suggestion

Sign in to comment.

Answers (2)

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
Ammar Ahmed
Ammar Ahmed on 13 Jun 2019

0 votes

thanks for answering but as i see still i need to use P = linspace(0.20, 0.27); command which resulting in counting all points in between [0.20 , 0.27]
Does for loop command applicable to generate smooth line between the points
i need just the four point [0.20,0.23,025,0.27] connect in smooth line between them

2 Comments

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.
thanks walter for your assisstant

Sign in to comment.

Categories

Find more on Interpolation in Help Center and File Exchange

Asked:

on 12 Jun 2019

Commented:

on 14 Jun 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!