1 view (last 30 days)

hi, i have a code in which in a for loop the cumulative sum of the equation needs to be used in the next loop and then put into another equation but i am having issues with matrix/array sizes.

This is my code:

A=9.41e-17;

n=5.5;

m=0.02;

B=1.054e-13;

phi=6.5;

x=4.44;

sigma=80;

%time stetup

timei=0;

timef=100;

dt=0.02;

timestep=(timef/dt)+1;

time=linspace(timei,timef,timestep);

for jj=1:timestep

if jj==0

w=0;

end

t=time(jj);

dw=B*(sigma/((1-w*(jj))^phi))*(t^m)*dt;

w=cumsum(dw);

de(jj)=(A*((sigma/((1-w))^n))*t^m*dt);

e=cumsum(de);

end

For this calculation the cumulative value of w has to be used to find the next change in w, the cumulative sum of w then has to be used to find the change in e and then the sumulative sum of e is then plotted against the time vector.

The issue i am having is when i create the vector dw by adding (jj) after it this error appears:

Incorrect dimensions for raising a matrix to a power. Check that the matrix is square and the power is a scalar. To perform elementwise

matrix powers, use '.^'.

Error in TEST (line 33)

de(jj)=(A*((sigma/((1-w))^n))*t^m*dt);

if i use'.^' the matrix dimensions do not agree.

Any advice is welcome, thanks

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

Start Hunting!
## 3 Comments

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/663468-using-the-previous-cumsum-value-of-a-for-loop-to-calculate-a-new-value#comment_1162678

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/663468-using-the-previous-cumsum-value-of-a-for-loop-to-calculate-a-new-value#comment_1162678

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/663468-using-the-previous-cumsum-value-of-a-for-loop-to-calculate-a-new-value#comment_1162833

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/663468-using-the-previous-cumsum-value-of-a-for-loop-to-calculate-a-new-value#comment_1162833

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/663468-using-the-previous-cumsum-value-of-a-for-loop-to-calculate-a-new-value#comment_1162953

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/663468-using-the-previous-cumsum-value-of-a-for-loop-to-calculate-a-new-value#comment_1162953

Sign in to comment.