For loop in matrix
정보
이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.
이전 댓글 표시
I'd like to create the matrix by two loops.
I tried to make the form below. Using "For loop"function, how can I make the code?
A=1;%dV/dx when x=1,A(0)=1
V=2;%V when x=1,V(0)=1
a=0; b=1;
n=100;
x=linspace(a,b,n);
h=x(n+1)-x(n);
V(i+1)=V(i)-A(i)-h.^2;% when i is from 0 to n
A(i+1)=A(i)+h;% when i is from 0 to n
plot(transpose(x),V,'r')
댓글 수: 0
답변 (1개)
A=1;%dV/dx when x=1,A(0)=1
V=2;%V when x=1,V(0)=1
a=0; b=1;
n=100;
x=linspace(a,b,n);
h = x(2)-x(1) ;
V = zeros(size(x)) ;
A = zeros(size(x)) ;
for i = 1:n-1
V(i+1)=V(i)-A(i)-h.^2;% when i is from 0 to n
A(i+1)=A(i)+h;% when i is from 0 to n
end
plot(x,V,'r')
댓글 수: 4
sookyung Kang
2020년 7월 11일
KSSV
2020년 7월 11일
You cannot...in MATLAB indices always start from 1. If you want 0 to n, it should be 1 to n+1.
sookyung Kang
2020년 7월 11일
KSSV
2020년 7월 11일
You cannot use it man.....instead use
V(1) = 2 ;
A(1) = 1 ;
이 질문은 마감되었습니다.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!