INTERP2 WITH LOOP FOR

1 view (last 30 days)
Pedro Franco
Pedro Franco on 23 Jan 2018
Commented: John D'Errico on 23 Jan 2018
%I_Phase1 [0 51.1111 102.2222 153.3333 204.4444 255.5556 306.6667 357.7778 408.8889 460]
%Theta1 [-45 -30 -15 0 15 30 45 60 75 90]
%Torque_Matrix [0 0 0 0 0 0 0 0 0 0 28.6989 35.9452 41.1581 43.8173 43.5092 40.0174 33.4011 24.0388 12.6221 0.0940 52.6956 67.7241 79.8022 87.4465 89.2066 84.0131 71.5044 52.2439 27.7582 0.3762 71.9900 95.3367 115.9323 130.8876 137.0923 131.9869 114.3100 84.6153 45.4083 0.8464 86.5822 118.7830 149.5483 174.1406 187.1661 183.9389 161.8178 121.1530 65.5724 1.5047 96.4722 138.0630 180.6504 217.2055 239.4282 239.8691 214.0278 161.8569 88.2505 2.3511 101.6600 153.1768 209.2385 260.0824 293.8785 299.7776 270.9400 206.7272 113.4425 3.3856 102.1456 164.1242 235.3126 302.7711 350.5170 363.6642 332.5544 255.7637 141.1486 4.6082 97.9289 170.9054 258.8727 345.2718 409.3438 431.5290 398.8711 308.9666 171.3687 6.0188 89.0100 173.5203 279.9188 387.5844 470.3588 503.3720 469.8900 366.3357 204.1028 7.6176]
I have two vectors (I_Phase1, Theta1) and one matrix (Torque_Matrix), I need to do a interpolation with interp2, but my fuction and for loop is not working. I need help, please.
surf(Theta1,I_Phase1,Torque_Matrix);
hold on
for Theta_Int = -45:90;
for I_Phase_Int = 0:460;
Torque_Value = interp2(Theta1,I_Phase1,Torque_Matrix,Theta_Int,I_Phase_Int);
end
end
surf(Theta_Int,I_Phase_Int,Torque_Value,'.r')

Accepted Answer

John D'Errico
John D'Errico on 23 Jan 2018
Edited: John D'Errico on 23 Jan 2018
Not sure why you are using a loop, when interp2 can interpolate the whole set in one call. But making your code reasonably efficient was not the question.
The problem here resides in your call to interp2, or rather what you do with the result.
On every pass through the loop, you overwrite torque_value, which is just a scalar. So you throw what was in there from the last pass through in the bit bucket.
Torque_Value = interp2(Theta1,I_Phase1,Torque_Matrix,Theta_Int,I_Phase_Int);
How can you expect MATLAB to know that you really wanted it to remember the ENTIRE set of numbers as an array?
So, instead, you might create torque_value as a matrix of the final size in advance. So start with this:
Torque_value = zeros(461,136);
Then assign each element to the proper spot as you compute it. Far better would be to use interp2 as it is designed to work, but that really is a different question.
  1 Comment
John D'Errico
John D'Errico on 23 Jan 2018
I did not say you "need" to delete the for loop. I said that would make it more efficient if done properly. You DO want to store the result using an index into the array Torque_value, if you will use a loop.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements 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!