For loops and vectorisation?

1 view (last 30 days)
Charrizard harrizard
Charrizard harrizard on 23 Oct 2014
Edited: Patrik Ek on 23 Oct 2014
a) Write a script which uses for loops to calculate the resistance of the copper wire at the five lengths. Display the results in two columns, one for the length and one for the resistance
b) Write additional script which uses vectorisation to calculate the resistance of the copper wire at the five lengths
  4 Comments
Charrizard harrizard
Charrizard harrizard on 23 Oct 2014
Sorry I've gotten the vectorisation down I just don't see how fprintf or for loops come into the problem,
resistivity = 1.68*(10.^-8);
length = 2; diameter = 0.001;
area = (pi*(diameter.^2));
disp('Area')
disp(area)
disp('Percentage elongation = pe')
pe = [ 0 3 6 9 12 ]
postlength =((pe+100)/100)*length;
disp('Lengths of copper wire after elongation')
disp(postlength)
disp('resistance at 5 wire lengths')
R = resistivity*postlength/area;
disp ( R )
That is basically what I've got so far
Patrik Ek
Patrik Ek on 23 Oct 2014
Edited: Patrik Ek on 23 Oct 2014
I am not sure about part b, but for part a: Assuming that you have a inhomogeneous leader of some length and with some width that depend on x and y. Also assuming that you have a measured voltage from end to end and also have a measured magnetic field at a radius r from the center. The answer would be far too long to write here, but as a hint you use Amperes law to calculate the current. And to Charrizard harrizard: you have assumed that there is a homogeneous wire and DC. It is extremly likely that the wire is homogeneous in both width and materialbut it is not given :). However, the frequency should be given to make an appropriate estimation.

Sign in to comment.

Accepted Answer

Adam
Adam on 23 Oct 2014
Well, if you went straight to the vectorisation solution it is a bit silly to also require a for loop solution, but I guess if those are the questions you have to answer you have to do the reverse of what is usually done and instead of operating on your vector of values all at once just put in a loop from 1 upto the length of the vector (array) and basically do the same maths, but with array indexing for the single element being worked on in the current loop.
As a silly example that you should be able to derive from for your example:
a = [1 2 3 4 5];
b = a + 6;
is obviously trivial vectorisation. However if you wanted to be inefficient you could equally do it as:
a = [1 2 3 4 5];
b = zeros( size(A) );
for i = 1:5
b(i) = a(i) + 6;
end
Your question doesn't seem to say you have to use fprintf, it is a function useful for writing formatted data to file. sprintf would be the equivalent to just create a string for display purposes.

More Answers (0)

Categories

Find more on Signal Generation and Preprocessing in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!