adding for loop with linspace with many variables

조회 수: 5 (최근 30일)
dana akil
dana akil 2022년 7월 24일
댓글: dana akil 2022년 7월 24일
Hi ,
I am trying to run this script but keep having errors ,please help
wg=0.18;
ws=0.12;
a=0.425;
b=2.353;
c=0.06;
d=0.18;
e=0.12;
p1 = 0.015604;
p2 = -1.4201;
p3 = 53.8951;
p4 = -1091.1818;
p5 = 12425.3578;
p6 = -75422.3333;
p7 = 190602.4449;
qp1 = 1.1584;
qp2 = -110.2867;
qp3 = 4362.3758;
qp4 = -91752.2953;
qp5 = 1082176.2735;
qp6 = -6786040.7706;
qp7 = 17674223.7811;
%x= linspace(10,30,20); %pitta
r= sqrt(410-(x.^2));
rr= (r).^2;
y1= @(x) (r.*wg);
%A = @(x) p1*x.^6 + p2*x.^5 + p3*x.^4 + p4*x.^3 + p5*x.^2 + p6.*x + p7 ;
%B= @(x) qp1*x.^6 + qp2*x.^5 +qp3*x.^4 + qp4*x.^3 +qp5*x.^2 + qp6.*x + qp7;
R = sqrt(235.72-rr);
T= @(x) 2.353*(R./r);
x= linspace(10,30,20);
for i=10:20r(i)
r(i)= sqrt(410-(i.^2));
rr(i)= (r(i)).^2;
A(i) = p1*i.^6 + p2*i.^5 + p3*i.^4 + p4*i.^3 + p5*i.^2 + p6.*i + p7 ;
B(i)= qp1*i.^6 + qp2*i.^5 +qp3*i.^4 + qp4*i.^3 +qp5*i.^2 + qp6.*i + qp7;
R(i) = sqrt(235.72-rr(i));
T(i)= 2.353*(R(i)./r(i));
yup(i) = A5(x).*T(x)+B5(x);
end
ydown=@(x) A5-B5.*T;
y2= yup./ydown;
y3= atan(y2)+2*pi;
plot (x,y1(x))
hold on
plot (x,A(x))
plot (x,B(x))
  댓글 수: 4
dana akil
dana akil 2022년 7월 24일
Conversion to function_handle from double is not possible.
dana akil
dana akil 2022년 7월 24일
I just need it to calculate all the variables one by one
for i=10
r(10) is
rr(10 ) is then calculate A(10 )
it keeps giving me one value

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

답변 (2개)

Alberto Cuadra Lara
Alberto Cuadra Lara 2022년 7월 24일
편집: Alberto Cuadra Lara 2022년 7월 24일
Hello Dana,
There are inconsistencies in your code:
  • x is not defined before use it
  • The loop is not properly defined, for i = 10 : 20 * r(i) ??
  • I do not see why you need to use a for loop here. MATLAB works great using vectorization.
  • A5 and B5 are not defined.
Please, clarify these points.
Best,
Alberto
  댓글 수: 1
dana akil
dana akil 2022년 7월 24일
I removed the for loop and it works fine
Thank you
But I have adiffrent proplem now
Array indices must be positive integers or logical values.
at y2 line
wg=0.18;
ws=0.12;
a=0.425;
b=2.353;
c=0.06;
d=0.18;
e=0.12;
p1 = 0.015604;
p2 = -1.4201;
p3 = 53.8951;
p4 = -1091.1818;
p5 = 12425.3578;
p6 = -75422.3333;
p7 = 190602.4449;
qp1 = 1.1584;
qp2 = -110.2867;
qp3 = 4362.3758;
qp4 = -91752.2953;
qp5 = 1082176.2735;
qp6 = -6786040.7706;
qp7 = 17674223.7811;
%x= linspace(10,30,20); %pitta
r= sqrt(410-(x.^2));
rr= (r).^2;
y1= @(x) (r.*wg);
A = @(x) p1*x.^6 + p2*x.^5 + p3*x.^4 + p4*x.^3 + p5*x.^2 + p6.*x + p7 ;
B= @(x) qp1*x.^6 + qp2*x.^5 +qp3*x.^4 + qp4*x.^3 +qp5*x.^2 + qp6.*x + qp7;
R = sqrt(235.72-rr);
T= 2.353*(R/r);
x= linspace(10,30,20);
r= sqrt(410-(x.^2));
rr = (r).^2;
yup = @(x) A(x)*T+B(x);
ydown=@(x) A(x)-B(x)*T;
figure(1)
%plot(x , yup(x))
%hold on
%plot(x , ydown(x))
y2(x)= real(yup(x)/ydown(x));
y3(x)= real(atan(y2)+2*pi);
figure(2)
plot (x,y3(x))
plot (x,y1(x))
hold on

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


Alberto Cuadra Lara
Alberto Cuadra Lara 2022년 7월 24일
편집: Alberto Cuadra Lara 2022년 7월 24일
Hi Dana,
I'm not sure if this is what you expect, but here is the code without errors. I have changed the handle functions to vectors.
% Constants
wg = 0.18;
ws = 0.12;
a = 0.425;
b = 2.353;
c = 0.06;
d = 0.18;
e = 0.12;
p1 = 0.015604;
p2 = -1.4201;
p3 = 53.8951;
p4 = -1091.1818;
p5 = 12425.3578;
p6 = -75422.3333;
p7 = 190602.4449;
qp1 = 1.1584;
qp2 = -110.2867;
qp3 = 4362.3758;
qp4 = -91752.2953;
qp5 = 1082176.2735;
qp6 = -6786040.7706;
qp7 = 17674223.7811;
% Definitions
x = linspace(10, 30, 20);
% Calculations
r = sqrt(410 - (x.^2));
rr = r.^2;
A = p1*x.^6 + p2*x.^5 + p3*x.^4 + p4*x.^3 + p5*x.^2 + p6.*x + p7 ;
B = qp1*x.^6 + qp2*x.^5 + qp3*x.^4 + qp4*x.^3 + qp5*x.^2 + qp6.*x + qp7;
R = sqrt(235.72 - rr);
T = 2.353*(R ./ r);
yup = A .* T + B;
ydown = A - T .* B;
y1 = real(r * wg);
y2 = real(yup ./ ydown);
y3 = real(atan(y2) + 2*pi);
% Plot figure
figure;
hold on;
plot (x, y3)
plot (x, y1)

카테고리

Help CenterFile Exchange에서 Performance and Memory에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by