Plot function with varying variable

8 Ansichten (letzte 30 Tage)
Joshua Yacoby
Joshua Yacoby am 19 Okt. 2020
Bearbeitet: Stephen23 am 19 Okt. 2020
Hello,
I am fairly new to Matlab and having an issue with a script I'm writing. The goal is to plot a few variations of function f on the same graph. The plot will have f on the y axis and z on the x axis. Function f is a function of z and y, and function f itself depends on the value of z. I want to plot for z=1 through 5. Below is what I have so far and I'm having trouble figuring out the problem. Thanks in advance for any help! (My actual functions are a bit more complicated than the example below, but with the solution to this example I should be able to apply)
for z=1:1:5
y=2
if z<=1
f=z*y
elseif z>1
f=2*z*y
end
plot(z,f)
hold on
end

Antworten (1)

Stephen23
Stephen23 am 19 Okt. 2020
Bearbeitet: Stephen23 am 19 Okt. 2020
The MATLAB approach is to use logical indexing, e.g.:
z = 0:0.1:5;
y = 2;
f = z*y;
idx = z>1; % logical index
f(idx) = 2*z(idx)*y;
plot(z,f,'-x')

Kategorien

Mehr zu 2-D and 3-D Plots finden Sie in Help Center und File Exchange

Produkte


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by