Plotting multiple points on a graph using for loop

Hi. I would like to plot multiple points on a graph using for loop and subplot. I have an xi value of 0.2 to 0.8 with increments of 0.2. And the for loop is used to go through each xi value. The legend can be hardcoded and is positioned in the 'best' location. The result should look like the first subplot below.
The equation is given by the pic below
This is what i've done so far.
clc;clear all;close all
y = @(t,xi,omega) 1-exp^-xi*omega*t/sqrt(1-xi^2)*sin(omega*sqrt(1-xi^2)*t+acos(xi));
omega = 10.26;
t = linspace(0,1,200);
%plotting
xi= 0.2:0.2:0.8;
n = length(xi);
for i=1:n
subplot(2,1,1)
plot(xi,y)
end
Your help is very much appreciated! Thank you.

 採用された回答

SHIVAM KUMAR
SHIVAM KUMAR 2021 年 2 月 2 日
編集済み: SHIVAM KUMAR 2021 年 2 月 4 日

0 投票

Final version
clc
clear all
close all
fn= @(xi,omega,t) (1-((exp(-xi*omega*t))/sqrt(1-xi^2)).*sin(omega*sqrt(1-xi^2)*t+acos(xi)));
omega = 10.26;
t = linspace(0,1,200);
%plotting
x1= 0.2:0.2:0.8;
n = length(x1);
figure;
subplot 211
for i=1:n
Y = fn(x1(i),omega,t);
plot(t,Y)
hold on
end
legend("xi=0.2","xi=0.4","xi=0.6","xi=0.8")
ylim([0 2])
hold off
title("Omega=10.26")
xlabel("t")
ylabel("y")
%for second plot
next= fn(0.65,omega,t);
subplot 212
plot(t,next)
ylim([0 1.2])
hold on
legend("step response")
The indexes were not nicely choosen in function. I corrected that now though.

9 件のコメント

SHIVAM KUMAR
SHIVAM KUMAR 2021 年 2 月 2 日
The image I got
Batrisyia Mohamad Asri
Batrisyia Mohamad Asri 2021 年 2 月 2 日
Thank you so much for your help!
Batrisyia Mohamad Asri
Batrisyia Mohamad Asri 2021 年 2 月 3 日
hi, i have to use anonymous function in my code. Could you please tell me which part i should modify. Thank you!
SHIVAM KUMAR
SHIVAM KUMAR 2021 年 2 月 3 日
I have edited my code just that you have to use brackets or it will cause error. Everything remains the same just added the function.
SHIVAM KUMAR
SHIVAM KUMAR 2021 年 2 月 3 日
You needed to use the function name instead of y.
Batrisyia Mohamad Asri
Batrisyia Mohamad Asri 2021 年 2 月 4 日
hello, i tried to run your code but matlab gives me error when i click run for the first time. But when i click run again for the second time, it gives me the desired answer. Not sure why this happens. hope you can help me out. thanks!
SHIVAM KUMAR
SHIVAM KUMAR 2021 年 2 月 4 日
I had messed up a bit with array positning. Have corrected that now.
It works well now.
Accept my answer if that's good to go :)
Batrisyia Mohamad Asri
Batrisyia Mohamad Asri 2021 年 2 月 5 日
Got it! Thank you so much!
SHIVAM KUMAR
SHIVAM KUMAR 2021 年 2 月 5 日
Accept my answer if that is correct.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Community Treasure Hunt

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

Start Hunting!

Translated by