Produce the same result but without using a for-loop.

% Produce the same result but without using a for-loop.
% Increment
dx = 35/300;
% Independent variable
x = -5:dx:30; % x = linspace(-5,30,300)
for n = 1:length(x)
if x(n) >= 9
y(n) = 15*sqrt(4*x(n)) + 10;
elseif (x(n) >= 0) && (x(n) <= 9)
y(n) = 10*x(n) + 10;
else
y(n) = 10;
end
end
plot(x,y), xlabel('x'), ylabel('y'), grid on

1 Commento

This time I edited your question for you. Next time, please use the tools explained on this page to make your question more readable.

Accedi per commentare.

Risposte (2)

Cris LaPierre
Cris LaPierre il 6 Apr 2021
Modificato: Cris LaPierre il 6 Apr 2021
This is a piecewise function. Perhaps the easiest way to do this is using the piecewise function.
If your assignment doesn't allow that, then create 3 vectors, one for each equation, and solve each for all values of x. Then multiply each by a logical array created from x and the corresponding conditional statement(s). Finally, add all three vectors together.
See Ch 12 of MATLAB Onramp if you need help creating the logical array, and Ch 6 if you need help on elementwise multiplication.

8 Commenti

i have used the recomended but gives me an error
and there is no error in the text or any were that i can see
Cris LaPierre
Cris LaPierre il 6 Apr 2021
Modificato: Cris LaPierre il 6 Apr 2021
Both techniques worked for me. Please share your code, as well as the error message (all the red text) that you are getting.
as you can see in the photo the error i dont know why
syms y(x)
y(x) = piecewise(x>=9, 15*sqrt(4*x)+10, 0>=x=<9, 10*x+10,10)
dx = 35/300;
xvalue = -5:dx:30;
fplot(y)
what should i do in this setuation?
Cris LaPierre
Cris LaPierre il 6 Apr 2021
Modificato: Cris LaPierre il 6 Apr 2021
I can't reproduce your error, but here's one suggestion.
You have not written your second condition in valid MATLAB syntax. You need two separate conditions joined with an '&', similar to what you did in the elseif line previously.
Also be aware of your endpoints of your different conditions. It is best if they are not shared. For example, if you have x>=9 in one, then the next range should be x<9 and not x<=9.
it worked when i did with & but its just ploting all of them as 10 so the line was straight for some reason but there is no error in the code anywere so i dont know what i couldve done wrong
and also i am sorry for being very annoying but thank you so much for your help
You have not written your second condition in valid MATLAB syntax. You need two separate conditions joined with an '&', similar to what you did in the elseif line previously.
That turns out not to be true for piecewise():
syms x
A = piecewise(2 <= x & x <= 3, 1, 0)
A = 
B = piecewise(2 <= x <= 3, 1, 0)
B = 
A - B
ans = 
0
Also be aware of your endpoints of your different conditions. It is best if they are not shared.
The rule for piecewise() is that the left-most case is the one that will be picked; which is to say that the first matching condition is the one that will be used. People overlap all the time, and it is often fine:
syms x
A = piecewise(x == 0, 2, -1 <= x & x <= 1, 1/x, 0)
A = 
However, when people have two matching boundary conditions, it is sometimes (but not always) a sign that they have no paid attention to be sure that the value matches at the boundary. When equations with bounds are extracted from textbooks or research papers, the authors will generally have ensured that the bounds match if the bounds are equal... but authors are known to make mistakes.
Cris LaPierre
Cris LaPierre il 6 Apr 2021
Modificato: Cris LaPierre il 6 Apr 2021
You have two competing conditions in your second case.
  • 0>=x means x less than or equal to 0.
  • x>=9 is the same as your condition for your first condition and means x greater than or equal to 9.
Can you see how the two conditions cannot be met? Think through how to define your ranges.
it worked you helped me alot thank you soooo much

Accedi per commentare.

syms y(x)
y(x) = piecewise(x>=9, 15*sqrt(4*x)+10, 0>=x=<9, 10*x+10,10)
^^
MATLAB does not have an =< operator; it has a <= operator.
dx = 35/300;
xvalue = -5:dx:30;
fplot(y)
fplot() by default plots from -5 to +5 . Your assignment to xvalue does not affect that. To have it plot over a different interval, the interval would have to be passed as the second parameter, such as
fplot(y, [-5, 30])
Only the endpoints of the interval would be passed.
fplot() always chooses its own points to plot at. If you only want to plot at particular points, you should not use fplot(); you should use plot() instead.
plot() does not accept formulas, so you would need to
plot(xvalue, y(xvalue))

Categorie

Scopri di più su Mathematics in Centro assistenza e File Exchange

Tag

Richiesto:

il 6 Apr 2021

Community Treasure Hunt

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

Start Hunting!

Translated by