Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How to get Max/Min values of array in MATAB ?

Status
Not open for further replies.

rahul.6sept

Full Member level 5
Joined
Nov 17, 2006
Messages
243
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,298
Location
Guwahati, India
Activity points
2,889
Dear all,

Kindly advice me the correction for this code of matlab.I'm not able to get the output. Also I don't know how to compare the outputs in array to find the max and min.


Code:
 %For x=[0:0.1:1], calculate f(x)=xe^-2x. Report the value of x at which
%f(x) is maximum. This value of x should be reported in variable "result"

x=[0:0.1:1];
fx=[];
while (x>=1)
       fx=x*exp(-2*x);
       disp (['the value of fx is =  ', num2str(fx)]); 
 end

Regards,

Rahul
 
Last edited by a moderator:

Code:
%For x=[0:0.1:1], calculate f(x)=xe^-2x. Report the value of x at which
%f(x) is maximum. This value of x should be reported in variable "result"

Why don't you do that by using the function max() already available on MATLAB standard install ?
 

Hi,

I tried to modify my code but it is still giving error.

Code:
%For x=[0:0.1:1], calculate f(x)=xe^-2x. Report the value of x at which
%f(x) is maximum. This value of x should be reported in variable "result"
x=[0:0.1:1];
fx=[];
result=[];

for i=1:length(x)
       fx(i)=x(i)*exp(-2*x(i));
       if fx(i)> fx(i-1)
           result(i)=fx(i);
           disp (['Cheers  ',num2str(result(i))]);
       else
           disp(['Sorry  ',num2str(fx(i))]);
       end
end

Sincerely
 

The code above seems like using the command for unnecessarily. Once MATLAB allows you perform the agebraic operation matricially, you could do something like that ( not tested ):

Code:
       fx=x.*exp(-2.*x);
       result=max(fx);
 

Your logic is wrong.

First compute fx. All the 10 values must be available in the array. Use one for loop for this purpose.

Next you find out the max value of the fx[] array. Use another for loop and locate the max value.

Finally you print out the max value outside the for loops. Job complete.

Study flowcharts. This will give you some basic ideas on programming.
 

Dear C_Mitra

Can u plz see my code as below. I made corrections as you said, but still display not coming correct.

Code:
%For x=[0:0.1:1], calculate f(x)=xe^-2x. Report the value of x at which
%f(x) is maximum. This value of x should be reported in variable "result"
x=[0:0.1:1];
fx=[];
result=[];

for i=1:length(x)
       fx=x.*exp(-2.*x);
       for i=1:fx[i]
         result=max(fx);
       end
end
 disp (['Cheers  ',num2str(result)]);

Regards,
Rahul Chakraborty
 

Code:
for i=1:length(x) fx(i)=x(i)*exp(-2*x(i)); end
% this is the long form what andre_teprom wrote: fx=x.*exp(-2.*x);
for i=1:length(x)-1; if(fx(i)>fx(i+1)) result=fx(i); end
% this is same as result=max(fx);
% you can print out result as you want...
 

Hi,

Here another code in which I'm getting the plot but it is plotting wrongly. The plot should have been a parabolic but it is plotting along a horizontal.
Code:
%Location of a ball thrown upwards is given by y=v0*t-1/2gt^2. Calculate the
%location of the ball for every 0.1 seconds until it reaches the ground
%(i.e y>0).Plot the graph with circles marked only at 0.4seconds interval.

%% Display of the ball hit vertically at every 0.1 seconds
v0 = 20;
g = 9.8;
y = [];
t =[0:0.04:3];
for i=1:length(t)
    t(i)=t(i)+0.1;
    y=v0*t(i)-(g*t(i)^2)/2;
    plot(t,y,'-bo');
end
 

please move the line containing the plot command outside the for ... end loop.

- - - Updated - - -

There are other mistakes too.

follow the suggestion given by andre_teprom; that is the reocmmended way for script.

Code:
y=v0*t.-0.5*g*t.*t;
plot (t,y,'-bo');

That is an elegant notation, right?

t.*t means that you ask it to multiply element by element. v0*t and v0*t. are same because v0 is a scalar.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top