user function and plot
2 次查看(过去 30 天)
显示 更早的评论
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
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
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
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Performance 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!