Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Debug my code please; Having trouble writing for loops

Subject: Debug my code please; Having trouble writing for loops

From: Chris Nassau

Date: 16 Apr, 2009 17:36:02

Message: 1 of 4

I am supposed to write a matlab program to calculate values of x. I am supposed to use the form "if elseif else" and the first line of code is supposed to be for x=1:2:5. When x is 1 you are supposed to calculate y=2x, when x=2 you are supposed to calculate y=sqrt(1)+sqrt(2) summation, when x=3 you are supposed to calculate y=sqrt(1)+sqrt(2)+sqrt(3) summation, and all other values within the range, such as 5 or 4 should print the message below.

Here is the code I have written:
    for x=1:2:5;
% Put your code here...
     x = input('Enter a number 1 through 5: ')
     if x < 2
         y = 2.*x
     elseif x > 3
         fprintf('MATLAB IS REALLY COOL')
     else
         for x = 2
             x = sqrt(j)
         end
         for x = 3
             x = sqrt (j)
         end
                       
    end;

    fprintf('\n')

Subject: Debug my code please; Having trouble writing for loops

From: Roger Stafford

Date: 16 Apr, 2009 18:22:02

Message: 2 of 4

"Chris Nassau" <chris15012@hotmail.com> wrote in message <gs7qa2$se0$1@fred.mathworks.com>...
> I am supposed to write a matlab program to calculate values of x. I am supposed to use the form "if elseif else" and the first line of code is supposed to be for x=1:2:5. When x is 1 you are supposed to calculate y=2x, when x=2 you are supposed to calculate y=sqrt(1)+sqrt(2) summation, when x=3 you are supposed to calculate y=sqrt(1)+sqrt(2)+sqrt(3) summation, and all other values within the range, such as 5 or 4 should print the message below.
>
> Here is the code I have written:
> for x=1:2:5;
> % Put your code here...
> x = input('Enter a number 1 through 5: ')
> if x < 2
> y = 2.*x
> elseif x > 3
> fprintf('MATLAB IS REALLY COOL')
> else
> for x = 2
> x = sqrt(j)
> end
> for x = 3
> x = sqrt (j)
> end
>
> end;
>
> fprintf('\n')

  There are many, many mistakes in your coding. Rather than our pointing them out to you tediously one after another it would be far better if you put some good hard study time into how such coding is supposed to be written. Look at your textbooks for examples of each of the aspects of the functions you propose to use. You can't learn by blindly putting down code you don't understand and hoping for the best. You have to know in advance how to accomplish the things you wish done. Also you can't learn by asking people in this newsgroup to do a complete debug for you, but you can learn by studying some of the answers given on other threads.

Roger Stafford

Subject: Debug my code please; Having trouble writing for loops

From: Rune Allnor

Date: 16 Apr, 2009 18:31:33

Message: 3 of 4

On 16 Apr, 19:36, "Chris Nassau" <chris15...@hotmail.com> wrote:
> I am supposed to write a matlab program to calculate
> values of x. =A0I am supposed to use the form "if elseif
> else" and the first line of code is supposed to be for x=3D1:2:5. =A0

There are a few too many errors and blunders
in your code to comment on, without writing
all your homework for you. And as a matter of
principle, we don't give canned solutions
to homework.

The one comment I will give on your code, is
that you make a habit of writing a semicolon, ';',
at the end of all lines. That way you don't get
as much clutter in the command window when you
run your script.

> When x is 1 you are supposed to calculate y=3D2x,
> when x=3D2 you are supposed to calculate y=3Dsqrt(1)+sqrt(2)
> summation, when x=3D3 you are supposed to calculate
> y=3Dsqrt(1)+sqrt(2)+sqrt(3) summation, and all other
> values within the range, such as 5 or 4 should print
> the message below.

One key step to finding a solution to a programming
problem is to come up with a verbal description
of exactly what the problem is supposed to do.

Try and modify the description above by substituting
the word 'if' for 'when', and you are a long step
closer to your program.

Rune

Subject: Debug my code please; Having trouble writing for loops

From: someone

Date: 16 Apr, 2009 18:47:01

Message: 4 of 4

"Chris Nassau" <chris15012@hotmail.com> wrote in message <gs7qa2$se0$1@fred.mathworks.com>...
> I am supposed to write a matlab program to calculate values of x. I am supposed to use the form "if elseif else" and the first line of code is supposed to be for x=1:2:5. When x is 1 you are supposed to calculate y=2x, when x=2 you are supposed to calculate y=sqrt(1)+sqrt(2) summation, when x=3 you are supposed to calculate y=sqrt(1)+sqrt(2)+sqrt(3) summation, and all other values within the range, such as 5 or 4 should print the message below.
>
> Here is the code I have written:
> for x=1:2:5;
> % Put your code here...
> x = input('Enter a number 1 through 5: ')
> if x < 2
> y = 2.*x
> elseif x > 3
> fprintf('MATLAB IS REALLY COOL')
> else
> for x = 2
> x = sqrt(j)
> end
> for x = 3
> x = sqrt (j)
> end
>
> end;
>
> fprintf('\n')

% I'll give you a hint in addition to what Roger has already said.
% The biggest problem with your code above is that you reassign
% the value of x inside the for loop. This is a **big** No-No.

% Simply comment-out the statement:
% x = input('Enter a number 1 through 5: ')

% That will get you further along.
% If you really need to input a number, change it to something like:
z = input('Enter a number 1 through 5: ')
% and use z in place of x in the above.
% ( But, IMHO the for loop won't make much sense.)

% Then look for other places where you reassign x within the for loop.
% And decide what you need to do in those cases.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us