How to resolve Undefined function or variable 'x' Issue

2 Ansichten (letzte 30 Tage)
John Albritton
John Albritton am 31 Okt. 2018
Bearbeitet: John Albritton am 31 Okt. 2018
I use the following code and get this error. *Undefined function or variable 't'.* What should I do to resolve this issue?
f1(t)=0.248*((t/0.075)-((sin(55.8*t))/(55.8*0.075))) f2(t)=0.248*(1-(1/(55.8*0.075)*((sin(55.8*t))-(sin(55.8)*(t-0.075)))))
for t=(0:0.01:3); if t<0.075 tmp=f1(t) else tmp=f2(t) end y=[y tmp] end
figure; plot(t,y)

Antworten (1)

Cam Salzberger
Cam Salzberger am 31 Okt. 2018
Bearbeitet: Cam Salzberger am 31 Okt. 2018
The error is because your first two lines are trying to use t before it is defined.
It appears that you are looking to compute y numerically, so in this case you could make f1 and f2 simple anonymous functions. The key here is to use the @ symbol.
f1 = @(x) 0.248*((x/0.075)-((sin(55.8*x))/(55.8*0.075)));
and similar for f2. I used x as the variable for the function to keep it distinct from the t you use later. It doesn't actually matter if they are the same variable or not, I just prefer to make them different for clarity.
This will be similar to if you wrote a separate function:
function y = f1(x)
y = 0.248*((x/0.075)-((sin(55.8*x))/(55.8*0.075)));
end
Anonymous functions just make it possible to do this in one line, and pass the function handle around to use wherever.
Also, I'm pretty sure that your code is vectorized as it is now, so instead of the for loop, you could just do:
t1 = 0:0.01:0.07;
t2 = 0.08:0.01:3;
y = [f1(t1) f2(t2)];
Or get even fancier with logical indexing:
t = 0:0.01:3;
y = [f1(t(t < 0.075)) f2(t(t >= 0.075))];
Or something that will work even if your t is out of order or something:
t = 0:0.01:3;
y = zeros(size(t));
whichF1 = t < 0.075
y(whichF1) = f1(t(whichF1));
y(~whichF1) = f2(t(~whichF1));
Any one of these will be faster than the loop, and will also prevent the inefficiency of building the array in the loop without preallocation, which slows things down a lot.
As an FYI, you can select your code in your post and click the "{}Code" button in the Answers editor, and it will indent it all for you so it appears as code and not regular text.
-Cam
  1 Kommentar
John Albritton
John Albritton am 31 Okt. 2018
Bearbeitet: John Albritton am 31 Okt. 2018
Cam, thank you for your help. My teacher instructed to use the for loop method. Is there a simple way to define t at the top and stick with using the for loop method? My goal is to make a plot from time 0s to 3s. Before 0.075 i need a positive slope linear plot and then once I hit 0.075, I need the plot to remain constant at the y value thru time 3. f1 is the function for the positive sloped portion, and f2 is for the constant portion.
f1(t)=0.248*((t/0.075)-((sin(55.8*t))/(4.18)))
f2(t)=0.248*(1-(1/4.18*((sin(55.8*t))-(sin(55.8)*(t-0.075)))))
for t=(0:0.01:3);
if t<0.075
tmp=f1(t)
else
tmp=f2(t)
end y=[y tmp]
end
figure;
plot(t,y)

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by