is it possible to make this code shorter?

I have coded something to calculate the needed spring stifness. now i want to make the code shorter because the code is a lot of copy past, only i dont know how/if it is possible to make it shorter. the code that i have made can you see here bellow, rad is an array.
r1=x(1)/2*cos(rad(1))
r2=x(2)/2*cos(rad(1)+rad(2))
r3=x(3)/2*cos(rad(1)+rad(2)+rad(3))
r4=x(4)/2*cos(rad(1)+rad(2)+rad(3)+rad(4))
r5=x(5)/2*cos(rad(1)+rad(2)+rad(3)+rad(4)+rad(5))
r6=x(6)/2*cos(rad(1)+rad(2)+rad(3)+rad(4)+rad(5)+rad(6))
r7=x(7)/2*cos(rad(1)+rad(2)+rad(3)+rad(4)+rad(5)+rad(6)+rad(7))
r21=2*r1+r2
r31=2*(r1+r2)+r3
r41=2*(r1+r2+r3)+r4
r51=2*(r1+r2+r3+r4)+r5
r61=2*(r1+r2+r3+r4+r5)+r6
r71=2*(r1+r2+r3+r4+r5+r6)+r7
mg1=Fz(1)*r1+Fz(2)*r21+Fz(3)*r31+Fz(4)*r41+Fz(5)*r51+Fz(6)*r61+Fz(7)*r71
mg2=Fz(2)*r21+Fz(3)*r31+Fz(4)*r41+Fz(5)*r51+Fz(6)*r61+Fz(7)*r71
mg3=Fz(3)*r31+Fz(4)*r41+Fz(5)*r51+Fz(6)*r61+Fz(7)*r71
mg4=Fz(4)*r41+Fz(5)*r51+Fz(6)*r61+Fz(7)*r71
mg5=Fz(5)*r51+Fz(6)*r61+Fz(7)*r71
mg6=Fz(6)*r61+Fz(7)*r71
mg7=Fz(7)*r71
mg=[mg1 mg2 mg3 mg4 mg5 mg6 mg7]
c=-mg./rad
If some one can help me with it or has a link to a page where this is explaind i will be very happy.

3 Comments

What is rad?
help rad
--- rad not found. Showing help for rand instead. --- RAND Uniformly distributed pseudorandom numbers. R = RAND(N) returns an N-by-N matrix containing pseudorandom values drawn from the standard uniform distribution on the open interval(0,1). RAND(M,N) or RAND([M,N]) returns an M-by-N matrix. RAND(M,N,P,...) or RAND([M,N,P,...]) returns an M-by-N-by-P-by-... array. RAND returns a scalar. RAND(SIZE(A)) returns an array the same size as A. Note: The size inputs M, N, P, ... should be nonnegative integers. Negative integers are treated as 0. R = RAND(..., CLASSNAME) returns an array of uniform values of the specified class. CLASSNAME can be 'double' or 'single'. R = RAND(..., 'like', Y) is an array of uniform values with the same data type and complexity (real or complex) as the numeric variable Y. The sequence of numbers produced by RAND is determined by the settings of the uniform random number generator that underlies RAND, RANDI, and RANDN. Control that shared random number generator using RNG. Examples: Example 1: Generate values from the uniform distribution on the interval (a, b). r = a + (b-a).*rand(100,1); Example 2: Use the RANDI function, instead of RAND, to generate integer values from the uniform distribution on the set 1:100. r = randi(100,1,5); Example 3: Reset the random number generator used by RAND, RANDI, and RANDN to its default startup settings, so that RAND produces the same random numbers as if you restarted MATLAB. rng('default') rand(1,5) Example 4: Save the settings for the random number generator used by RAND, RANDI, and RANDN, generate 5 values from RAND, restore the settings, and repeat those values. s = rng u1 = rand(1,5) rng(s); u2 = rand(1,5) % contains exactly the same values as u1 Example 5: Reinitialize the random number generator used by RAND, RANDI, and RANDN with a seed based on the current time. RAND will return different values each time you do this. NOTE: It is usually not necessary to do this more than once per MATLAB session. rng('shuffle'); rand(1,5) See Replace Discouraged Syntaxes of rand and randn to use RNG to replace RAND with the 'seed', 'state', or 'twister' inputs. See also RANDI, RANDN, RNG, RANDSTREAM, RANDSTREAM/RAND, SPRAND, SPRANDN, RANDPERM. Documentation for rand doc rand Other uses of rand codistributed/rand distributed/rand qrandstream/rand codistributor1d/rand gpuArray/rand RandStream/rand codistributor2dbc/rand matlab/rand
Fz is also not defined.
Please show all the info necessary to help.
rad is an array that i used like sayd in the question.
I forgot to metion that Fz is also an array.
both the arrays are a 1x7.
the numbers in the array can always be changed.
their for it will not be that helpfull if i show the arrays.

Sign in to comment.

 Accepted Answer

Even if I could make the code you show shorter, I would not do so, not to create that huge mess of variable names. EVER. Anyway, you cannot shorten anything, because you need all of those lines, because you are using numbered variable names.
Instead, replace all the cases where you create the numbered variables r1,r2,r3,r4... into ONE line. LEARN TO WRITE USING MATLAB.
r = x/2.*cos(cumsum(rad));
that creates a vector r. NOT 7 different numbered variable names. You already know how to use vectors. SO DO IT.
Next, you create a list of 6 more numbered variables, r21, r31, r41, ...
Learn from what I just said. You might now do this in one more line. Again, a vector is created.
rsum = 2*cumsum(r(2:end)) + r(1:end-1);
Finally, you create now 7 MORE numbered variable names, mg1,mg2,mg3.... All just to combine them into a vector at the end! DON'T DO IT. You are still thinking in terms of writing spreadsheets when you do this.
mg = flip(cumsum(flip(Fz.*[r(1),rsum])));
Again, it took only 3 lines of code to write. Your last line is the only line I would leave unchanged.
c = -mg./rad;

More Answers (2)

One function that will be of use to you is cumsum. This will let you avoid variables with numbered names like r1, r2, r3, etc. In general defining a numbered sequence of variables is discouraged.
r=x/2.*cos(cumsum(rad));
R=[r(1),2*cumsum(r(1:6))+r(2:7)];
mg=flip(cumsum(flip(Fz.*R)));
c=-mg./rad;

Products

Release

R2021b

Community Treasure Hunt

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

Start Hunting!