adding for loop with linspace with many variables

3 Ansichten (letzte 30 Tage)
dana akil
dana akil am 24 Jul. 2022
Kommentiert: dana akil am 24 Jul. 2022
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 Kommentare
dana akil
dana akil am 24 Jul. 2022
Conversion to function_handle from double is not possible.
dana akil
dana akil am 24 Jul. 2022
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

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Alberto Cuadra Lara
Alberto Cuadra Lara am 24 Jul. 2022
Bearbeitet: Alberto Cuadra Lara am 24 Jul. 2022
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 Kommentar
dana akil
dana akil am 24 Jul. 2022
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

Melden Sie sich an, um zu kommentieren.


Alberto Cuadra Lara
Alberto Cuadra Lara am 24 Jul. 2022
Bearbeitet: Alberto Cuadra Lara am 24 Jul. 2022
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)

Kategorien

Mehr zu Performance and Memory finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by