where to write function

조회 수: 1 (최근 30일)
shiv gaur
shiv gaur 2022년 2월 2일
댓글: Voss 2022년 2월 2일
m=0;
k0 = 1; la0 = 2*pi/k0; a = 0.3;
ec = 2.2^2; es = 1.7^2; ef = -4;
pc = ef/ec; ps = ef/es;
tol = 1e-12;
bsinf = sqrt(ef*es/(ef+es));
x = linspace(-3,3,601)*a;
plot(x/a, real(Hy), linewidth,2);
function [be,E] = pwga(la0,ef,ec,es, a, bsinf, m, tol);
ga = sqrt(be^2-ef); as = sqrt(be^2-es); ac = sqrt(be^2-ec);
psi = atanh(-pc*ac/ga)/2- atanh(-ps*as/ga)/2
Hy = cosh(ga*a - psi).*exp(as*(x+a)).*(x<-a) + ...
cosh(ga*a + psi).*exp(-ac*(x-a)).*(x>a) + ...
cosh(ga*x + psi).*(abs(x)<=a);
end
pl plot the following
  댓글 수: 8
shiv gaur
shiv gaur 2022년 2월 2일
pl help to update the function that creates a variable to call function
Steven Lord
Steven Lord 2022년 2월 2일
Try updating the function yourself (it's a good way to learn.) If you are not able to successfully do so post your updated function and we can offer more feedback.

댓글을 달려면 로그인하십시오.

채택된 답변

Voss
Voss 2022년 2월 2일
Sorry, I had the impression that the question was where to write the function, based on the title, "where to write function".
If the question is how to plot real(Hy) vs x/a, then you're going to have to get Hy out of that function or plot from inside the function (both of which would require calling the function at some point). But also, there is an undefined variable be you'd have to define first. I'll just pick be = 1 to show how it might work:
m=0;
k0 = 1;
la0 = 2*pi/k0;
a = 0.3;
ec = 2.2^2;
es = 1.7^2;
ef = -4;
pc = ef/ec;
ps = ef/es;
tol = 1e-12;
bsinf = sqrt(ef*es/(ef+es));
x = linspace(-3,3,601)*a;
Hy = pwga(la0,ef,ec,es,pc,ps,a,bsinf,m,tol,x);
plot(x/a, real(Hy), 'linewidth',2);
function Hy = pwga(la0,ef,ec,es,pc,ps,a,bsinf,m,tol,x)
be = 1;
ga = sqrt(be^2-ef);
as = sqrt(be^2-es);
ac = sqrt(be^2-ec);
psi = atanh(-pc*ac/ga)/2- atanh(-ps*as/ga)/2;
Hy = cosh(ga*a - psi).*exp(as*(x+a)).*(x<-a) + ...
cosh(ga*a + psi).*exp(-ac*(x-a)).*(x>a) + ...
cosh(ga*x + psi).*(abs(x)<=a);
end
In this case the input argument names in the definition of the function are the same as the names of the variables the function is called with, but this need not be the case, and it's important to understand that if you want to understand how functions work. For example, you can try the following to see how it works:
first_a = 100;
first_b = 200;
second_a = 1000;
second_b = 2000;
add_two_numbers(first_a,first_b)
add_two_numbers(second_a,second_b)
function c = add_two_numbers(a,b)
c = a+b;
end
  댓글 수: 3
shiv gaur
shiv gaur 2022년 2월 2일
when be ask this question again then this will be duplicate so you are req to give some help
Voss
Voss 2022년 2월 2일
How would I know what the value of be is supposed to be?
You could maybe calculate be from a given value of Hy, but no Hy is given in your code; in fact Hy is what was calculated from be.
Perhaps some description on your part about what the various variables represent, or the field or domain of problem would help people have some context and maybe an expert in that field can assist you.
If you want to avoid your questions getting flagged as duplicates, then you should ask a clear question each time, so it's obvious to others how the question is different from other questions relating to the same piece of code.
I am req to do nothing.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

태그

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by