How to plot graphic with 2 variables function
이전 댓글 표시

I use these codes:
clear
clc
H=2.5
h=11
T=9
w=2*pi/T
k1=1
g=9.81
%This is Newton's Rhapson Method
x1=k1*h;
TOL=1.0e-5;
f=@(x) x*tanh(x)-w^2*h/g;
df=@(x) tanh(x)+(x/(cosh(x))^2);
fprintf ('Iteration root f(root) \n')
if df(x1) ~= 0
for i=1:1000
x2=x1-(f(x1)/df(x1));
fprintf ('ke-%d %.5f %.5f\n', [i,x1,f(x1)]')
if abs (f(x2)) < TOL
break
end
x1=x2;
end
end
fprintf ('root: %f\n',x1')
fprintf ('Number of iteration: %f\n', i')
L=2*pi()*h/x1
% PLOT
kgel=x1/h
xi=-L/4
xf=5*L/4
t=0
fplot(@(x) H/2 * cos(kgel.*x - w*t),[xi xf],'Color','c','LineWidth',1.5)
title('u and w in t=0')
xlabel('x axis')
ylabel('z axis')
grid
nx1=L/4
nx2=3*L/4
nx3=L/2
nx4=L
hold on
fplot(@(z) (H*w/2)*(cosh(kgel*(h+z))/sinh(kgel*h))*cos(kgel*nx1-w*t),[-h H])
fplot(@(z) (H*w/2)*(sinh(kgel*(h+z))/sinh(kgel*h))*sin(kgel*nx1-w*t),[-h H])
hold off
What should i do?
went wrong like this:

댓글 수: 7
Dyuman Joshi
2023년 3월 9일
There are undefined variables in your code, thus we can not run it to check the results.
Please update the code.
Bagus
2023년 3월 9일
Dyuman Joshi
2023년 3월 9일
Okay, so you have to plot u (x-velocity) and v (z-velocity) vs x,z, and t? If so, how is that a 2D plot?
If not, please explain further.
Bagus
2023년 3월 9일
Bagus
2023년 3월 9일
Bagus
2023년 3월 9일
How did you come to the conclusion that u and v at t=0 are given by
H/2 * cos(kgel.*x - w*t)
The magnitude of u (~10^-16) is quite small compared to v (~1), see the plots at the end. Are the numerical values all correct? I suggest you to cross-check.
H=2.5;
h=11;
T=9;
w=2*pi/T;
k1=1;
g=9.81;
%This is Newton's Rhapson Method
x1=k1*h;
TOL=1.0e-5;
f=@(x) x*tanh(x)-w^2*h/g;
df=@(x) tanh(x)+(x/(cosh(x))^2);
Also, it's not clear what you want to do here with the if condition.
if df(x1) ~= 0
for i=1:1000
x2=x1-(f(x1)/df(x1));
if abs (f(x2)) < TOL
break
end
x1=x2;
end
end
L=2*pi*h/x1;
% PLOT
kgel=x1/h;
xi=-L/4;
xf=5*L/4;
t=0;
fplot(@(x) H/2 * cos(kgel.*x - w*t),[xi xf],'Color','c','LineWidth',1.5)
title('u and w in t=0')
xlabel('x axis')
ylabel('z axis')
grid
nx1=L/4;
nx2=3*L/4;
nx3=L/2;
nx4=L;
%x-velocity
u = @(z) (H*w/2)*(cosh(kgel*(h+z))/sinh(kgel*h))*cos(kgel*nx1-w*t);
%y-velocity
v = @(z) (H*w/2)*(sinh(kgel*(h+z))/sinh(kgel*h))*sin(kgel*nx1-w*t);
figure
fplot(u,[-h H])
figure
fplot(v,[-h H])
답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


