How to better use cells?
Ältere Kommentare anzeigen
Hello, I'm bad at MATLAB but am trying to get better. I had an assignment where I was supposed to solve a system of equations using the Forward Euler method and plot over time. I had never used cells before and my partner got that portion working. I spent a bunch of time reading through help files and this forum and I still don't understand how to implement cells well. I think I'm getting confused with how I should be writing to and reading from cells.
Code:
u(1) = 1; v(1) = 0;
dt = .01;
n = 1;
t = 0:dt:10;
A = [-1 -1;1 -1];
I = [1 0;0 1];
Y = cell (size(t));
u1 = 1; v1 = 0;
Y1 = [u1 ; v1];
Y{n} = [u(n) ; v(n)];
while n <= numel(t)-1
Y{n+1} = (I+dt*A)*Y{n};
n = n+1;
end
Y = Y(1,:);
Ytograph=zeros(size(Y));
for idx=1:numel(Y)
Temp=Y{idx};
Ytograph(idx)=Temp(2);
end
plot(t, Ytograph)
It think it gets messy after the while loop and I'm hoping to get some tips on how to make it better/more efficient.
Thank you!
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!