Hi guys, I'm new to MatLab and I would like to know how I could use the "for" and "while" command in the following problem:
Make two programs in matlab, one applying the "for" repetition structure and the other "while" structure, to plot a function y = ax ^ 2 + bx + c to x varying from -5 to 5 with a step of 0.1 in 0.1 , using the "input" function to input values of a, b and c. The program should repeat the process N times.
Thanks!

5 Comments

This sounds like a homework assignment. If it is, show us the code you've written to try to solve the problem and ask a specific question about where you're having difficulty and we may be able to provide some guidance.
If you aren't sure where to start because you're not familiar with how to write MATLAB code, I suggest you start with the MATLAB Onramp tutorial (https://www.mathworks.com/support/learn-with-matlab-tutorials.html) to quickly learn the essentials of MATLAB.
If you aren't sure where to start because you're not familiar with the mathematics you'll need to solve the problem, I recommend asking your professor and/or teaching assistant for help.
x=[-5:0.1:5];
a=input('Insira o valor de a:');
b=input('Insira o valor de b:');
c=input('Insira o valor de c:');
for N=-5:0.1:5
y(x)=a*x.^2+b*x+c;
end
plot(x,y), grid
title ('Gráfico do exercício 3')
xlabel ('x')
ylabel ('f(x)')
I tried this, but I'm not sure if it's right.
Jan
Jan on 28 Apr 2021
What do you see, when you run it? The body of the loop does not use the loop counter N anywhere. Then the loops is useless. You try to use the vector x as index of y, but indices must be positive integers, while some elements of x are not.
x=[-5:0.1:5];
a=input('Insira o valor de a:');
b=input('Insira o valor de b:');
c=input('Insira o valor de c:');
for i=1:100
y(x)=a*x.^2+b*x+c;
plot(x,y), grid
title ('Gráfico do exercício 3')
xlabel ('x')
ylabel ('f(x)')
end
And this?
You are still misunderstanding the purpose of the loop. If we go back to your first attempt, have a look at this and see if you can see what is happening. Then try to amend your second attempt (which is a more conventional way of doing it) to do the same thing, but using i counter in your loop. And you should really take Steven Lord's advice and go through the Matlab Onramp course - it really will make things much clearer and easier.
a=input('Insira o valor de a:');
b=input('Insira o valor de b:');
c=input('Insira o valor de c:');
iterator=1;
for N=-5:0.1:5
y(iterator)=a*N^2+b*N+c;
iterator=iterator+1;
end
plot(-5:0.1:5,y)
title ('Gráfico do exercício 3')
xlabel ('x')
ylabel ('f(x)')

Sign in to comment.

 Accepted Answer

Jan
Jan on 28 Apr 2021
Start with the smallest possible problem to understand, what the loops do.
for k = 1:5
disp(k)
end
Please run this in your command window. Does this explain, what the loop does?
WHILE and FOR loops are equivalent:
k = 1;
while k <= 5
disp(k);
k = k + 1;
end
FOR loops are useful, if you know in advance how many iterations you will get. If this is not clear, use a WHILE loop:
iter = 0;
ready = false;
while ~ready
iter = iter + 1;
x = input('Type in a number: ');
if x > 5
ready = true;
end
end
Now to your code:
x=[-5:0.1:5];
for N=-5:0.1:5
y(x)=a*x.^2+b*x+c;
end
[ and ] are the Matlab operator for a concatenation. -5:0.1:5 is a vector already and you concatenate it with nothing. So x=-5:0.1:5 is sufficient already.
y(x) means the x.th element of y. Therefore x must be a positive integer, but this is not the case e.g. for -5 or -4.9 . David has explained this already. One of the solutions:
x = -5:0.1:5;
for k = 1:numel(x)
y(k) = a * x(k) .^ 2 + b * x(k) + c;
end
You can do this without a loop in Matlab also:
x = -5:0.1:5;
y = a * x .^ 2 + b * x + c;
plot(x, y);
But I assume, the idea of this homework is to learn how to use loops.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 28 Apr 2021

Answered:

Jan
on 28 Apr 2021

Community Treasure Hunt

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

Start Hunting!