Implicit function inside a loop.

조회 수: 6 (최근 30일)
Anshuman S
Anshuman S 2018년 1월 21일
댓글: Anshuman S 2018년 1월 21일
I want to plot a curve between err and b for different values of b.
  • I have kept the values of i to be a constant for the inner iteration, and I want to sum up the error ( square )
  • In 'o' and 'p'for different values of 'i' and finally I want to plot err vs b. such that i get a minimum to this curve at a certain 'b'.
sum =0;
err =0;
for b= 0:1:40
for i= 0:1:50
f1 = @(o,i) (sind(b+i) + sind(b-o)) -( 1.180/0.04 + sqrt.((1.140/0.04 - 2*sind(b)).^2 - (cosd(b-o)- cosd(b+i)).^2));
f2 = @(p,i) cotd(p-3.34) - cotd(i-1.07) - (1.180/(1.540 - 0.300));
sum = sum + (o - p).^2;
end
err = sqrt(sum/50);
plot (b,err);
hold on
end
  댓글 수: 1
Anshuman S
Anshuman S 2018년 1월 21일
I want to use these equations to get the values of 'o' and 'p' which are to be solved for same value of 'i'. Now that i have 'o' and 'p' values in terms of 'b' . I want to sum this for all the 'i's in the inner loop . further when i come out of the loop I want to plot a point (b, err) , the value of b comes from the outer loop. hence i'll get a set of 40 points for err vs b and i'll draw a curve to fit these data.
Thanks

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2018년 1월 21일
total = 0;
err = 0;
bvals = 0:1:40;
for b = bvals
subtotal = 0;
for i= 0:1:50
f1 = @(o,i) (sind(b+i) + sind(b-o)) -( 1.180/0.04 + sqrt.((1.140/0.04 - 2*sind(b)).^2 - (cosd(b-o)- cosd(b+i)).^2));
f2 = @(p,i) cotd(p-3.34) - cotd(i-1.07) - (1.180/(1.540 - 0.300));
subtotal = subtotal + (o - p).^2;
end
err = sqrt(subtotal/50);
plot(bvals,err);
hold on
total = total + subtotal;
end
But why do you define f1 and f2 when you do not use them? And you have not defined o or p but you try to add their squared difference to the total?
  댓글 수: 3
Anshuman S
Anshuman S 2018년 1월 21일
The inner loop varies for 'i' and I want to work (subtotal = subtotal + (o - p).^2) with those values of 'o' and 'p' which come out from the equation f1 and f2 respectively.
Anshuman S
Anshuman S 2018년 1월 21일
I want to use these equations to get the values of 'o' and 'p' which are to be solved for same value of 'i'. Now that i have 'o' and 'p' values in terms of 'b' . I want to sum this for all the 'i's in the inner loop . further when i come out of the loop I want to plot a point (b, err) , the value of b comes from the outer loop. hence i'll get a set of 40 points for err vs b and i'll draw a curve to fit these data.
Thanks.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Linear and Nonlinear Regression에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by