# program is not showing graph

1 view (last 30 days)
shiv gaur on 11 Jan 2022
Answered: Walter Roberson on 11 Jan 2022
function metal3
theta1=linspace(10,70,20);
%T3=1e-9:1e-9:1e-6;
for j=1:numel(theta1)
theta=theta1(j);
p0 = 0.5;
p1 = 1;
p2 = 1.5;
TOL = 10^-8;
N0 = 100;
format long;
h1 = p1 - p0;
h2 = p2 - p1;
DELTA1 = (f(p1,theta) - f(p0,theta))/h1;
DELTA2 = (f(p2,theta) - f(p1,theta))/h2;
d = (DELTA2 - DELTA1)/(h2 + h1);
i=3;
while i <= N0
b = DELTA2 + h2*d;
D = (b^2 - 4*f(p2,theta)*d)^(1/2);
if abs(b-D) < abs(b+D)
E = b + D;
else
E = b - D;
end
h = -2*f(p2,theta)/E;
p = p2 + h;
if abs(h) < TOL
p
disp(p)
break
end
p0 = p1;
p1 = p2;
p2 = p;
h1 = p1 - p0;
h2 = p2 - p1;
DELTA1 = (f(p1,theta) - f(p0,theta))/h1;
DELTA2 = (f(p2,theta) - f(p1,theta))/h2;
d = (DELTA2 - DELTA1)/(h2 + h1);
i = i + 1;
end
if i > N0
formatSpec = string('The method failed after N0 iterations,N0= %d \n');
end
P(j)=real(p);
end
plot(theta ,r)
end
function y=f(x,theta)
k0=(2*pi/0.6328)*1e6;
n0=1.3707;
n1=1.30;
n2=1.59;
n3=0.065+4*1i;
n4=3.48;
na=1.36;
t1=2.10e-6;
t2=0.198e-6;
t3=1e-6;
t4=0.002e-6;
x=n0*k0*sin(theta);
k1=k0*sqrt(n1.^2-x.^2);
k2=k0*sqrt(n2.^2-x.^2);
k3=k0*sqrt(n3.^2-x.^2);
k4=k0*sqrt(n4.^2-x.^2);
m11= cos(t1*k1)*cos(t2*k2)-(k2/k1)*sin(t1*k1)*sin(t2*k2);
m12=(1/k2)*(cos(t1*k1)*sin(t2*k2)*1i) +(1/k1)*(cos(t2*k2)*sin(t1*k1)*1i);
m21= (k1)*cos(t2*k2)*sin(t1*k1)*1i +(k2)*cos(t1*k1)*sin(t2*k2)*1i;
m22=cos(t1*k1)*cos(t2*k2)-(k1/k2)*sin(t1*k1)*sin(t2*k2);
m34= cos(t3*k3)*cos(t4*k4)-(k4/k3)*sin(t3*k3)*sin(t4*k4);
m32=(1/k4)*(cos(t3*k3)*sin(t4*k4)*1i) +(1/k3)*(cos(t4*k4)*sin(t3*k3)*1i);
m23= (k3)*cos(t4*k4)*sin(t3*k3)*1i +(k4)*cos(t3*k3)*sin(t4*k4)*1i;
m33=cos(t3*k3)*cos(t4*k4)-(k3/k4)*sin(t3*k3)*sin(t4*k4);
M11=m11*m34+m12*m23;
M12=m11*m32+m12*m33;
M21=m21*m34+m22*m23;
M22=m21*m32+m22*m33;
g0=sqrt(x.^2-n0.^2)*k0;
ga= sqrt(x.^2-na.^2)*k0;
y=(na^2*g0*M11-n0^2*ga*M22+g0*ga*M12-na^2*n0^2*M21)/(na^2*g0*M11+n0^2*ga*M22+g0*ga*M12+na^2*n0^2*M21);
r=y.^2;
end
##### 2 CommentsShowHide 1 older comment
shiv gaur on 11 Jan 2022
not working

Walter Roberson on 11 Jan 2022
You have
function y=f(x,theta)
You are passing in a variable named x
k0=(2*pi/0.6328)*1e6;
That's a value in the millions
n0=1.3707;
n1=1.30;
That's a value near 1
n2=1.59;
n3=0.065+4*1i;
n4=3.48;
na=1.36;
t1=2.10e-6;
t2=0.198e-6;
t3=1e-6;
t4=0.002e-6;
x=n0*k0*sin(theta);
You have not used the x that you passed in. Instead you have overwritten x with a value that is determined in part by k0, which is in the millions, so x is going to end up in the millions.
k1=k0*sqrt(n1.^2-x.^2);
Remember n1 is near 1, and you are subtracting a million squared, and taking the square root. So you are going to end up with a value that is roughly complex 1 million.
m11= cos(t1*k1)*cos(t2*k2)-(k2/k1)*sin(t1*k1)*sin(t2*k2);
cos of complex 1 million is infinite . Remember that cos of an imaginary number has to do with exponentials .

R2021b

### Community Treasure Hunt

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

Start Hunting!
{"ft_lang_label":"__label__en","ft_lang_prob":0.57060534,"math_prob":0.99977714,"size":3025,"snap":"2022-05-2022-21","text_gpt3_token_len":1414,"char_repetition_ratio":0.12876531,"word_repetition_ratio":0.1573604,"special_character_ratio":0.50380164,"punctuation_ratio":0.17192982,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":1.0,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-05-28T08:27:28Z\",\"WARC-Record-ID\":\"<urn:uuid:61e06850-9af8-4359-8d0f-ace4533e9bbe>\",\"Content-Length\":\"145123\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:51aa08f1-9176-4ea8-bd51-67b874d2e17e>\",\"WARC-Concurrent-To\":\"<urn:uuid:6e0dbbd2-8390-4724-aa87-55ae168dc7ae>\",\"WARC-IP-Address\":\"23.1.9.244\",\"WARC-Target-URI\":\"https://au.mathworks.com/matlabcentral/answers/1626620-program-is-not-showing-graph\",\"WARC-Payload-Digest\":\"sha1:A3MLMIZSKQZWSHFPBB32ZAJEGVHXPE3I\",\"WARC-Block-Digest\":\"sha1:JDS3LJ43YHQDF5QNG3WYUIJPD5DUGSI5\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-21/CC-MAIN-2022-21_segments_1652663013003.96_warc_CC-MAIN-20220528062047-20220528092047-00417.warc.gz\"}"}