Index exceeds the number of array elements (1).

1 visualizzazione (ultimi 30 giorni)
Yixuan LU
Yixuan LU il 20 Mar 2019
Risposto: Krishna Zanwar il 22 Mar 2019
When I want to run and plot the quadratic solver, I met the error 'Index exceeds the number of array elements (1)'.
% Some code to show you how to handle arithmatic involving complex numbers
clc
clear
% We'll write some code that solves a simple quadratic equation.
% The roots of the quadratic can either be real or complex and this depends
% upon the coefficients in a the original equation.
%
% Equation has the form:
% ax^2+bx+c=0
%
% Coefficient 'c' varied in magnitude so that a mix of real and complex
% roots are generated. Top tip - the above equation could be the characteristic
% equation of the car and 'c' could be vehicle speed.
%
% Initialise the variables that define the roots of our quadratic (defined
% as complex numbers)
x1com=complex(0.0,0.0);
x2com=complex(0.0,0.0);
%
M_ul = 1995; Md = 60; I = 3877;
M = M_ul + Md;
l_f = 1.34; l_r = 1.12;l_wb = 2.46; Rmean = 60; t = 1.62;
K_f = 55000; K_r = 65000;u=1;
% Specify the values of the coefficients of the quadratic
a=1;
b=(2*M*(l_f^2*K_f+l_r^2*K_r)+2*I*(K_f+K_r))/M*u*I;
c=((4*K_f*K_r*l_wb^2)/(M*I*u^2))-(2*(l_f*K_f-l_r*K_r)/I);
%
for u=1:50
c=c(1+u);
b=b(u+1);
x1com=(-b+sqrt(b^2-4*a.*c))/(2*a);
x2com=(-b-sqrt(b^2-4*a.*c))/(2*a);
% Extract the real and imaginary parts from the roots of the quadratic
% (which have been calculated as complex numbers)
% Place these into real vectors
x1real(u)=real(x1com);
x1imag(u)=imag(x1com);
x2real(u)=real(x2com);
x2imag(u)=imag(x2com);
end
%
% Generate some graphs to illustrate results using the real vectors
% 1) Re vs Im
subplot(3,1,1)
plot(x1real(:),x1imag(:),'ko',x2real(:),x2imag(:),'b+')
% axis([xmin xmax ymin ymax]) % Use this to define axis limits
legend('Root 1','Root 2','Location','EastOutside')
title('Roots of quadratic equation');
grid on
xlabel('Real')
ylabel('Imaginary')
% 2) Re vs c
subplot(3,1,2)
plot(c(:),x1real(:),'ko',c(:),x2real(:),'b+')
% axis([xmin xmax ymin ymax]) % Use this to define axis limits
legend('Root 1','Root 2','Location','EastOutside')
grid on
xlabel('Coefficient c')
ylabel('Real')
% 3) Im vs c
subplot(3,1,3)
plot(c(:),x1imag(:),'ko',c(:),x2imag(:),'b+')
% axis([xmin xmax ymin ymax]) % Use this to define axis limits
legend('Root 1','Root 2','Location','EastOutside')
grid on
xlabel('Coefficient c')
ylabel('Imaginary')
%
% Save the graph as a .jpg and place in working directory
saveas(gcf,'examplegraph.jpg','jpg');
  5 Commenti
Yixuan LU
Yixuan LU il 20 Mar 2019
b and c are the variables against u and what I want is to get matrix of b and c when u increaseing so that I can get a series of results for the quadratic equation
Dennis
Dennis il 20 Mar 2019
You only calculate the values of b and c for u=1. Maybe you want to change
%u=1;
u=1:50;
and
% c=c(1+u);
% b=b(u+1);
% x1com=(-b+sqrt(b^2-4*a.*c))/(2*a);
% x2com=(-b-sqrt(b^2-4*a.*c))/(2*a);
x1com=(-b(u)+sqrt(b(u)^2-4*a.*c(u)))/(2*a);
x2com=(-b(u)-sqrt(b(u)^2-4*a.*c(u)))/(2*a);

Accedi per commentare.

Risposte (1)

Krishna Zanwar
Krishna Zanwar il 22 Mar 2019
In the code above 'b' and 'c' are scalars and cannot be indexed. If you want to get an array of 'b' and 'c' as 'u' is increasing you sould put them inside the for loop.
b(u)=(2*M*(l_f^2*K_f+l_r^2*K_r)+2*I*(K_f+K_r))/M*u*I;
c(u)=((4*K_f*K_r*l_wb^2)/(M*I*u^2))-(2*(l_f*K_f-l_r*K_r)/I);
x1com=(-b(u)+sqrt(b(u)^2-4*a*c(u)))/(2*a);
x2com=(-b(u)-sqrt(b(u)^2-4*a*c(u)))/(2*a);

Prodotti


Release

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by