I'm trying to recreate the graph above using for loop code.
I got the outline done but don't know how to plot the lines inside the curve. The for loop below is incorrect. Could anyone come up with the correct for loop for this?
hold on
plot([1,0,0,1,1],[0,0,1,1,0],'g')
x = 0:0.025:1;
y = (1. - sqrt(x)).^2;
for n=1:40
plot([n/40,0],[0,n/40],k)
end

 채택된 답변

Voss
Voss 2024년 2월 25일

0 개 추천

hold on
x = 0:0.025:1;
y = flip(x);
for n=1:numel(x)
plot([x(n),0],[0,y(n)],'k')
end
plot([1,0,0,1,1],[0,0,1,1,0],'g')

댓글 수: 5

Or without the for loop:
hold on
x = 0:0.025:1;
x(2,:) = 0;
y = flip(flip(x,2),1);
% y = x(end:-1:1,end:-1:1); % alternative to flip(flip(_))
plot(x,y,'k');
plot([1,0,0,1,1],[0,0,1,1,0],'g')
Khoi
Khoi 2024년 2월 25일
Thank you Voss
Voss
Voss 2024년 2월 25일
You're welcome! Any questions, let me know. Otherwise, please "Accept" this answer. Thanks!
Khoi
Khoi 2024년 2월 26일
is there any way for me to keep this line of code and add on to it using for loop?
hold on
plot([1,0,0,1,1],[0,0,1,1,0],'g')
x = 0:0.025:1;
y = (1. - sqrt(x)).^2;
I'm not sure what you mean, but you can plot that x and y after the other stuff, sure.
hold on
x = 0:0.025:1;
y = flip(x);
for n=1:numel(x)
plot([x(n),0],[0,y(n)],'k')
end
plot([1,0,0,1,1],[0,0,1,1,0],'g')
y = (1. - sqrt(x)).^2;
plot(x,y,'LineWidth',4)

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

추가 답변 (0개)

카테고리

태그

질문:

2024년 2월 25일

댓글:

2024년 2월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by