user function and plot

조회 수: 3 (최근 30일)
N/A
N/A 2022년 11월 17일
답변: VBBV 2022년 11월 17일
function [x1,x2]=distance_plot(W)
prompt1=('input value of W(N) = ') %W값 입력받기
prompt2=('input value of d(m) = ') %d값 입력받기
W=input(prompt1);
d=input(prompt2);
k1=104; %단위:N/m
k2=1.5*10^4; %단위:N/m
W0=linspace(0,W,1000);
x=0;
if x<d
x1=W/k1;
else x>=d
x2=(W+2*k2*d)/(k1+2*k2);
end
if x<d
x=linspace(0,d,1000);
plot(x1,W0)
xlabel('x1')
ylabel('W')
grid
else x>=d
x=linspace(0,d,1000);
plot(x2,W0)
xlabel('x2')
ylabel('W')
grid
end
(Don't care about the comments)
hello
i made a code(used defined function).
i thought about drawing a graph with a specified range according to the value of 'W' and 'd'.
there is no error code but graph didn't appear in the figure window.
Is it wrong to put the input value 'W' and 'd' directly into the ( W0=linspace(0,W,1000); ) and ( x=linspace(0,d,1000); )?
any other wrong?
thanks!
  댓글 수: 1
Rik
Rik 2022년 11월 17일
You're already writing a function, so why not have W and d both as input arguments? You're currently overwriting your input argument W in that input statement.

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

채택된 답변

VBBV
VBBV 2022년 11월 17일
May be this change, with regards to returning output variables x1 and x2 since function is not returning both variables as written
[x1 x2] = distance_plot
x1 = 0.0962
x2 = 1×1000
0 0.0040 0.0080 0.0120 0.0160 0.0200 0.0240 0.0280 0.0320 0.0360 0.0400 0.0440 0.0480 0.0521 0.0561 0.0601 0.0641 0.0681 0.0721 0.0761 0.0801 0.0841 0.0881 0.0921 0.0961 0.1001 0.1041 0.1081 0.1121 0.1161
function [x1,x2]=distance_plot % empty or no argument since inputs for W and d are fed after function call
W = 10; %input value for W e.g
d = 4; % input value for d e.g
k1=104; %단위:N/m
k2=1.5*10^4; %단위:N/m
W0=linspace(0,W,1000);
x=0;
if x<d % common condition
x1=W/k1;
x2=linspace(0,d,1000);
plot(x2,W0)
xlabel('x1')
ylabel('W')
grid
else x>=d % common condition
x2=(W+2*k2*d)/(k1+2*k2);
x1=linspace(0,d,1000);
plot(x1,W0)
xlabel('x2')
ylabel('W')
grid
end
end

추가 답변 (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