How to multiply different elements of a vector(with no value stored) inside a for loop to generate an equation.
조회 수: 1 (최근 30일)
이전 댓글 표시
I want to repeatedly add one form of the equation i times, in this I want constants as f(1),f(2)... and variables x1, x2...y1,y2...z1,z2...So, the required outcome for i=1 should be g=f(1).*x1.^2+f(2).*y1+f(3).*z1 as seen by program below(not working). It is necessary for its use with nlinfit.
I am trying to implement it using for loop with null vector(supposedly wrong approach). I have also tried declaring it as f(1)=[]... it also gives an error "Deletion requires an existing variable". It works if I use f as a symbol like x,y or z but that will be useless for me.
Please give me a solution to the problem addressed not the problems in my solution. Thanks :)
x=sym('x',[1 3]);
y=sym('y',[1 3]);
z=sym('z',[1 3]);
f=[]; %first way to declare null vector
%f=int16.empty(3,0); %second way to declare null vector
g=0;
a=1;
for i=1:1:3
g=g+f(i).*x(i).^2+f(i+1).*y(i)+f(i+2).*z(i);
a=a+3;
end
댓글 수: 5
KALYAN ACHARJYA
2019년 12월 27일
편집: KALYAN ACHARJYA
2019년 12월 27일
f=[];
If you multiply it with any,still it reflects a null vector. In this case there is no meaning of f(i) within loop iteration, which is f(1), f(2)...so on.
답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!