The plot of this discontinous values does not work.

Dear Matlab Community,
I got a problem plotting this discontinous geometry. There are no erros, but The plot is empty, just filled with some dots, that are even hard to realize :-( I really need to plot with x defined as a vector using linspace, like I did. I have no clue how to solve this problem. I would be very thankful if someone could help me out.
Thank you! Stefan
Here is the code I try to plot:
clc
clear all
close all
%Parameter:
R=2;
h=1;
L=10;
B = 5;
x=linspace(0.0,(2*R+L),100);
hmin = h/2;
if (x < R)
y2 = (R+hmin)-sqrt((R+hmin)^2-((R+hmin)^2-R^2+(x-R).^2));
y2_neg = -y2;
elseif (x > (L+R))
y2 = (R+hmin)-sqrt((R+hmin)^2-((R+hmin)^2-R^2+(x-R).^2));
y2_neg = -y2;
else
y2 = hmin;
y2_neg = -y2;
end
plot(x,y2,x,y2_neg);
grid on
title('Geometrie der Gelenkkontur')
xlabel('s = [0:L]')
ylabel('H(x)')
axis equal

 채택된 답변

Erik S.
Erik S. 2015년 2월 18일

0 개 추천

I send you a file, is it the result you need?

댓글 수: 3

Thank you very much, this works perfectly fine!
I had to correct my code in here:
elseif (x(n) > (L+R))
y2(n) = (R+hmin)-sqrt((R+hmin)^2-((R+hmin)^2-R^2+(x(n)-L-R).^2));
where I changed x(n)-R to x(n)-L-R and now I ged the exact correct solution. Thanks a lot for helping!
Great! Glad I could help! :) Pls Click on the Accept Answer button :)
Sure :) Again, thanks a lot!

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

추가 답변 (2개)

Erik S.
Erik S. 2015년 2월 18일

0 개 추천

Your y2 and y2_neg variables are not vectors, only scalars You can make a loop and compute the y2 and y2_neg for each value of x instead

댓글 수: 1

Thanks for the quick answer! I changed it to:
for x=0:0.1:(L+2*R)
if (x < R)
y2 = (R+hmin)-sqrt((R+hmin)^2-((R+hmin)^2-R^2+(x-R).^2));
y2_neg = -y2;
elseif (x > (L+R))
y2 = (R+hmin)-sqrt((R+hmin)^2-((R+hmin)^2-R^2+(x-R).^2));
y2_neg = -y2;
else
y2 = hmin;
y2_neg = -y2;
end
plot(x,y2,x,y2_neg);
grid on
title('Geometrie der Gelenkkontur')
xlabel('s = [0:L]')
ylabel('H(x)')
axis equal
end
But now I get Errors and an empty plot. I have to say I am new to matlab and do not find the problem here.. Thanks!

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

Erik S.
Erik S. 2015년 2월 18일

0 개 추천

There are a couple of issues. First you get complex numbers ( at least I get it when I run your code) so somehow you take the sqrt of a negative value. Secondly you overwrite your plot in every iteration. I would recommend to create vectors inside the loop (i.e. make y2 and y2_neg vectors) and run the plotting command outside the loop

카테고리

도움말 센터File Exchange에서 Graphics Performance에 대해 자세히 알아보기

질문:

2015년 2월 18일

댓글:

2015년 2월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by