Nested For Loop/Newton Raphson

I am trying to run carry out the Newton Raphson method for a series of Area Ratios in relation to Wind Tunnel experiments. x0 is an initial guess and the AR is an array of 25 area ratios. The method will converge on a solution. I need the for loop to run through all of the area ratios and calculate an X value for each, and return this in the array M for each ratio. Unfortunately I cant get this to work and will just repeat the same X value for all of the ratios at the end. Does anyone have any ideas how I could fix this?
x0 = 1.5; %x0 represents Mach Number Initial Guess
AR = [1.8652 1.5472 1.3327 1.1793 1.0827 1.0258 1.0016 1.0302 1.1091 1.1898 1.2653 1.3334 1.392 1.4389 1.4759 1.5044 1.5249 1.5376 1.5467 1.5547 1.5626 1.5704 1.5783 1.5862 1.5941]
f = @(x0) ((((5/6)+((1/6)*(x0^2)))^3))-(x0*AR);
df = @(x0)((x0))*(((5/6)+((1/6)*(x0^2)))^2)-AR;
M = [];
for i=AR
for i=1:10
x1 = x0 - f(x0)/df(x0);
x0 = x1;
disp(x1)
end
M=[M,(x1)];
end

Answers (1)

VBBV
VBBV on 14 Dec 2022
Edited: VBBV on 14 Dec 2022
x0 = 1.5; %x0 represents Mach Number Initial Guess
AR = [1.8652 1.5472 1.3327 1.1793 1.0827 1.0258 1.0016 1.0302 1.1091 1.1898 1.2653 1.3334 1.392 1.4389 1.4759 1.5044 1.5249 1.5376 1.5467 1.5547 1.5626 1.5704 1.5783 1.5862 1.5941]
AR = 1×25
1.8652 1.5472 1.3327 1.1793 1.0827 1.0258 1.0016 1.0302 1.1091 1.1898 1.2653 1.3334 1.3920 1.4389 1.4759 1.5044 1.5249 1.5376 1.5467 1.5547 1.5626 1.5704 1.5783 1.5862 1.5941
f = @(x0) ((((5/6)+((1/6)*(x0.^2))).^3))-(x0.*AR);
df = @(x0)((x0)).*(((5/6)+((1/6)*(x0.^2))).^2)-AR;
for k=1:length(AR)
x1 = x0 - f(x0)./df(x0);
x0 = x1;
disp(x1);
M{k}=x1;
end
Columns 1 through 19 4.6811 2.3657 1.7738 1.5046 1.3734 1.3063 1.2797 1.3112 1.4069 1.5204 1.6446 1.7753 1.9056 2.0246 2.1295 2.2180 2.2864 2.3309 2.3638 Columns 20 through 25 2.3936 2.4237 2.4542 2.4859 2.5185 2.5518 Columns 1 through 19 3.7981 2.0487 1.7021 1.5046 1.3392 1.2132 1.1519 1.2239 1.3898 1.5198 1.6193 1.7030 1.7785 1.8461 1.9065 1.9587 1.9999 2.0272 2.0476 Columns 20 through 25 2.0662 2.0852 2.1046 2.1250 2.1460 2.1678 Columns 1 through 19 3.0905 1.9165 1.6948 1.5046 1.3368 1.1861 1.0847 1.2009 1.3893 1.5198 1.6184 1.6956 1.7561 1.8024 1.8391 1.8684 1.8907 1.9051 1.9159 Columns 20 through 25 1.9256 1.9356 1.9457 1.9564 1.9675 1.9790 Columns 1 through 19 2.5694 1.8941 1.6948 1.5046 1.3367 1.1836 1.0544 1.1992 1.3893 1.5198 1.6184 1.6955 1.7554 1.7999 1.8333 1.8580 1.8754 1.8861 1.8937 Columns 20 through 25 1.9003 1.9069 1.9134 1.9199 1.9265 1.9332 Columns 1 through 19 2.2546 1.8935 1.6948 1.5046 1.3367 1.1836 1.0453 1.1992 1.3893 1.5198 1.6184 1.6955 1.7554 1.7999 1.8332 1.8579 1.8751 1.8856 1.8931 Columns 20 through 25 1.8996 1.9059 1.9121 1.9184 1.9246 1.9307 Columns 1 through 19 2.1345 1.8935 1.6948 1.5046 1.3367 1.1836 1.0443 1.1992 1.3893 1.5198 1.6184 1.6955 1.7554 1.7999 1.8332 1.8579 1.8751 1.8856 1.8931 Columns 20 through 25 1.8996 1.9059 1.9121 1.9184 1.9246 1.9307 Columns 1 through 19 2.1179 1.8935 1.6948 1.5046 1.3367 1.1836 1.0443 1.1992 1.3893 1.5198 1.6184 1.6955 1.7554 1.7999 1.8332 1.8579 1.8751 1.8856 1.8931 Columns 20 through 25 1.8996 1.9059 1.9121 1.9184 1.9246 1.9307 Columns 1 through 19 2.1176 1.8935 1.6948 1.5046 1.3367 1.1836 1.0443 1.1992 1.3893 1.5198 1.6184 1.6955 1.7554 1.7999 1.8332 1.8579 1.8751 1.8856 1.8931 Columns 20 through 25 1.8996 1.9059 1.9121 1.9184 1.9246 1.9307 Columns 1 through 19 2.1176 1.8935 1.6948 1.5046 1.3367 1.1836 1.0443 1.1992 1.3893 1.5198 1.6184 1.6955 1.7554 1.7999 1.8332 1.8579 1.8751 1.8856 1.8931 Columns 20 through 25 1.8996 1.9059 1.9121 1.9184 1.9246 1.9307 Columns 1 through 19 2.1176 1.8935 1.6948 1.5046 1.3367 1.1836 1.0443 1.1992 1.3893 1.5198 1.6184 1.6955 1.7554 1.7999 1.8332 1.8579 1.8751 1.8856 1.8931 Columns 20 through 25 1.8996 1.9059 1.9121 1.9184 1.9246 1.9307 Columns 1 through 19 2.1176 1.8935 1.6948 1.5046 1.3367 1.1836 1.0443 1.1992 1.3893 1.5198 1.6184 1.6955 1.7554 1.7999 1.8332 1.8579 1.8751 1.8856 1.8931 Columns 20 through 25 1.8996 1.9059 1.9121 1.9184 1.9246 1.9307 Columns 1 through 19 2.1176 1.8935 1.6948 1.5046 1.3367 1.1836 1.0443 1.1992 1.3893 1.5198 1.6184 1.6955 1.7554 1.7999 1.8332 1.8579 1.8751 1.8856 1.8931 Columns 20 through 25 1.8996 1.9059 1.9121 1.9184 1.9246 1.9307 Columns 1 through 19 2.1176 1.8935 1.6948 1.5046 1.3367 1.1836 1.0443 1.1992 1.3893 1.5198 1.6184 1.6955 1.7554 1.7999 1.8332 1.8579 1.8751 1.8856 1.8931 Columns 20 through 25 1.8996 1.9059 1.9121 1.9184 1.9246 1.9307 Columns 1 through 19 2.1176 1.8935 1.6948 1.5046 1.3367 1.1836 1.0443 1.1992 1.3893 1.5198 1.6184 1.6955 1.7554 1.7999 1.8332 1.8579 1.8751 1.8856 1.8931 Columns 20 through 25 1.8996 1.9059 1.9121 1.9184 1.9246 1.9307 Columns 1 through 19 2.1176 1.8935 1.6948 1.5046 1.3367 1.1836 1.0443 1.1992 1.3893 1.5198 1.6184 1.6955 1.7554 1.7999 1.8332 1.8579 1.8751 1.8856 1.8931 Columns 20 through 25 1.8996 1.9059 1.9121 1.9184 1.9246 1.9307 Columns 1 through 19 2.1176 1.8935 1.6948 1.5046 1.3367 1.1836 1.0443 1.1992 1.3893 1.5198 1.6184 1.6955 1.7554 1.7999 1.8332 1.8579 1.8751 1.8856 1.8931 Columns 20 through 25 1.8996 1.9059 1.9121 1.9184 1.9246 1.9307 Columns 1 through 19 2.1176 1.8935 1.6948 1.5046 1.3367 1.1836 1.0443 1.1992 1.3893 1.5198 1.6184 1.6955 1.7554 1.7999 1.8332 1.8579 1.8751 1.8856 1.8931 Columns 20 through 25 1.8996 1.9059 1.9121 1.9184 1.9246 1.9307 Columns 1 through 19 2.1176 1.8935 1.6948 1.5046 1.3367 1.1836 1.0443 1.1992 1.3893 1.5198 1.6184 1.6955 1.7554 1.7999 1.8332 1.8579 1.8751 1.8856 1.8931 Columns 20 through 25 1.8996 1.9059 1.9121 1.9184 1.9246 1.9307 Columns 1 through 19 2.1176 1.8935 1.6948 1.5046 1.3367 1.1836 1.0443 1.1992 1.3893 1.5198 1.6184 1.6955 1.7554 1.7999 1.8332 1.8579 1.8751 1.8856 1.8931 Columns 20 through 25 1.8996 1.9059 1.9121 1.9184 1.9246 1.9307 Columns 1 through 19 2.1176 1.8935 1.6948 1.5046 1.3367 1.1836 1.0443 1.1992 1.3893 1.5198 1.6184 1.6955 1.7554 1.7999 1.8332 1.8579 1.8751 1.8856 1.8931 Columns 20 through 25 1.8996 1.9059 1.9121 1.9184 1.9246 1.9307 Columns 1 through 19 2.1176 1.8935 1.6948 1.5046 1.3367 1.1836 1.0443 1.1992 1.3893 1.5198 1.6184 1.6955 1.7554 1.7999 1.8332 1.8579 1.8751 1.8856 1.8931 Columns 20 through 25 1.8996 1.9059 1.9121 1.9184 1.9246 1.9307 Columns 1 through 19 2.1176 1.8935 1.6948 1.5046 1.3367 1.1836 1.0443 1.1992 1.3893 1.5198 1.6184 1.6955 1.7554 1.7999 1.8332 1.8579 1.8751 1.8856 1.8931 Columns 20 through 25 1.8996 1.9059 1.9121 1.9184 1.9246 1.9307 Columns 1 through 19 2.1176 1.8935 1.6948 1.5046 1.3367 1.1836 1.0443 1.1992 1.3893 1.5198 1.6184 1.6955 1.7554 1.7999 1.8332 1.8579 1.8751 1.8856 1.8931 Columns 20 through 25 1.8996 1.9059 1.9121 1.9184 1.9246 1.9307 Columns 1 through 19 2.1176 1.8935 1.6948 1.5046 1.3367 1.1836 1.0443 1.1992 1.3893 1.5198 1.6184 1.6955 1.7554 1.7999 1.8332 1.8579 1.8751 1.8856 1.8931 Columns 20 through 25 1.8996 1.9059 1.9121 1.9184 1.9246 1.9307 Columns 1 through 19 2.1176 1.8935 1.6948 1.5046 1.3367 1.1836 1.0443 1.1992 1.3893 1.5198 1.6184 1.6955 1.7554 1.7999 1.8332 1.8579 1.8751 1.8856 1.8931 Columns 20 through 25 1.8996 1.9059 1.9121 1.9184 1.9246 1.9307
M.'
ans = 25×1 cell array
{[4.6811 2.3657 1.7738 1.5046 1.3734 1.3063 1.2797 1.3112 1.4069 1.5204 1.6446 1.7753 1.9056 2.0246 2.1295 2.2180 2.2864 2.3309 2.3638 2.3936 2.4237 2.4542 2.4859 2.5185 2.5518]} {[3.7981 2.0487 1.7021 1.5046 1.3392 1.2132 1.1519 1.2239 1.3898 1.5198 1.6193 1.7030 1.7785 1.8461 1.9065 1.9587 1.9999 2.0272 2.0476 2.0662 2.0852 2.1046 2.1250 2.1460 2.1678]} {[3.0905 1.9165 1.6948 1.5046 1.3368 1.1861 1.0847 1.2009 1.3893 1.5198 1.6184 1.6956 1.7561 1.8024 1.8391 1.8684 1.8907 1.9051 1.9159 1.9256 1.9356 1.9457 1.9564 1.9675 1.9790]} {[2.5694 1.8941 1.6948 1.5046 1.3367 1.1836 1.0544 1.1992 1.3893 1.5198 1.6184 1.6955 1.7554 1.7999 1.8333 1.8580 1.8754 1.8861 1.8937 1.9003 1.9069 1.9134 1.9199 1.9265 1.9332]} {[2.2546 1.8935 1.6948 1.5046 1.3367 1.1836 1.0453 1.1992 1.3893 1.5198 1.6184 1.6955 1.7554 1.7999 1.8332 1.8579 1.8751 1.8856 1.8931 1.8996 1.9059 1.9121 1.9184 1.9246 1.9307]} {[2.1345 1.8935 1.6948 1.5046 1.3367 1.1836 1.0443 1.1992 1.3893 1.5198 1.6184 1.6955 1.7554 1.7999 1.8332 1.8579 1.8751 1.8856 1.8931 1.8996 1.9059 1.9121 1.9184 1.9246 1.9307]} {[2.1179 1.8935 1.6948 1.5046 1.3367 1.1836 1.0443 1.1992 1.3893 1.5198 1.6184 1.6955 1.7554 1.7999 1.8332 1.8579 1.8751 1.8856 1.8931 1.8996 1.9059 1.9121 1.9184 1.9246 1.9307]} {[2.1176 1.8935 1.6948 1.5046 1.3367 1.1836 1.0443 1.1992 1.3893 1.5198 1.6184 1.6955 1.7554 1.7999 1.8332 1.8579 1.8751 1.8856 1.8931 1.8996 1.9059 1.9121 1.9184 1.9246 1.9307]} {[2.1176 1.8935 1.6948 1.5046 1.3367 1.1836 1.0443 1.1992 1.3893 1.5198 1.6184 1.6955 1.7554 1.7999 1.8332 1.8579 1.8751 1.8856 1.8931 1.8996 1.9059 1.9121 1.9184 1.9246 1.9307]} {[2.1176 1.8935 1.6948 1.5046 1.3367 1.1836 1.0443 1.1992 1.3893 1.5198 1.6184 1.6955 1.7554 1.7999 1.8332 1.8579 1.8751 1.8856 1.8931 1.8996 1.9059 1.9121 1.9184 1.9246 1.9307]} {[2.1176 1.8935 1.6948 1.5046 1.3367 1.1836 1.0443 1.1992 1.3893 1.5198 1.6184 1.6955 1.7554 1.7999 1.8332 1.8579 1.8751 1.8856 1.8931 1.8996 1.9059 1.9121 1.9184 1.9246 1.9307]} {[2.1176 1.8935 1.6948 1.5046 1.3367 1.1836 1.0443 1.1992 1.3893 1.5198 1.6184 1.6955 1.7554 1.7999 1.8332 1.8579 1.8751 1.8856 1.8931 1.8996 1.9059 1.9121 1.9184 1.9246 1.9307]} {[2.1176 1.8935 1.6948 1.5046 1.3367 1.1836 1.0443 1.1992 1.3893 1.5198 1.6184 1.6955 1.7554 1.7999 1.8332 1.8579 1.8751 1.8856 1.8931 1.8996 1.9059 1.9121 1.9184 1.9246 1.9307]} {[2.1176 1.8935 1.6948 1.5046 1.3367 1.1836 1.0443 1.1992 1.3893 1.5198 1.6184 1.6955 1.7554 1.7999 1.8332 1.8579 1.8751 1.8856 1.8931 1.8996 1.9059 1.9121 1.9184 1.9246 1.9307]} {[2.1176 1.8935 1.6948 1.5046 1.3367 1.1836 1.0443 1.1992 1.3893 1.5198 1.6184 1.6955 1.7554 1.7999 1.8332 1.8579 1.8751 1.8856 1.8931 1.8996 1.9059 1.9121 1.9184 1.9246 1.9307]} {[2.1176 1.8935 1.6948 1.5046 1.3367 1.1836 1.0443 1.1992 1.3893 1.5198 1.6184 1.6955 1.7554 1.7999 1.8332 1.8579 1.8751 1.8856 1.8931 1.8996 1.9059 1.9121 1.9184 1.9246 1.9307]}

1 Comment

Thanks very much this has solved my problem I believe, appreciate it.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Release

R2022a

Asked:

on 14 Dec 2022

Commented:

on 14 Dec 2022

Community Treasure Hunt

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

Start Hunting!