user function and plot

1 view (last 30 days)
Hakyoon Kim
Hakyoon Kim on 17 Nov 2022
Answered: VBBV on 17 Nov 2022
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 Comment
Rik
Rik on 17 Nov 2022
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.

Sign in to comment.

Accepted Answer

VBBV
VBBV on 17 Nov 2022
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

More Answers (0)

Categories

Find more on Environment and Settings in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!