How do I take a normal script with a function and turn it into a script with an anonymus function?

조회 수: 2 (최근 30일)
I am trying to use this old script I wrote and turn it into an anonymous function script using a loop. I am having some issues though, and I think I have just not refined ever variable and function to operate the way I need it to.
Here is the script I have worked on so far to change to anonymous:
if true
%
% Clear Window, Variables, and Figure
clear;
clc;
clf;
% Variables
time_interval = 0.04; % seconds
tVals = (-5:time_interval:1); % -5 to 1 in steps of 0.04
% Equation
y = @(t) t.^2+3.*t+4;
% Making a for loop
for k = 1:length(tVals)
yVals = y(tVals(k)); % t will take on the values of tVals
% Plot the result
plot(tVals(k),yVals,'-r'); % Plots t-values vs y-values; in default color (blue)
% Label the Graph
xlabel('Time (seconds)'); % Labels x-axis
ylabel('Distance (meters)'); % Labels y-axis
title('Time vs. Distance'); % Titles the graph
end
% Find min/max value of y and time when it occurs
[ymax, t1] = max(yVals);
[ymin, t2] = min(yVals);
% Print Part a
fprintf('The max value of y is %.2f meters and occurs at t=%.1f seconds \n', ymax, tVals(t1));
fprintf('The min value of y is %.2f meters and occurs at t=%.1f seconds \n', ymin, tVals(t2));
end
And here is the old script:
if true
%
% Clear Window, Variables, and Figure
clear;
clc;
clf;
% Variables
time_interval = 0.04; % seconds
t = [-5:time_interval:1]; % -5 to 1 in steps of 0.04
% Equation
y = t.^2+3.*t+4;
% Plot the result
plot(t,y); % Plots t-values vs y-values; in default color (blue)
% Label the Graph
xlabel('Time (seconds)'); % Labels x-axis
ylabel('Distance (meters)'); % Labels y-axis
title('Time vs. Distance'); % Titles the graph
% Find min/max value of y and time when it occurs
[ymax, t1] = max(y);
[ymin, t2] = min(y);
% Print Part a
fprintf('The max value of y is %.2f meters and occurs at t=%.1f seconds \n', ymax, t(t1));
fprintf('The min value of y is %.2f meters and occurs at t=%.1f seconds \n', ymin, t(t2));
end
The old function returns fine giving me the proper plot and min and max values, but the new script does not bring up the plot and returns the min and max values as the same numbers.
Any advice or help would be greatly appreciated.
Thank you.

채택된 답변

Star Strider
Star Strider 2015년 7월 21일
You can take advantage of vectorisation and your anonymous function to do it without a loop:
% Variables
time_interval = 0.04; % seconds
tVals = (-5:time_interval:1); % -5 to 1 in steps of 0.04
% Equation
y = @(t) t.^2+3.*t+4;
yVals = y(tVals); % t will take on the values of tVals
% Plot the result
plot(tVals, yVals, '-r'); % Plots t-values vs y-values; in default color (blue)
% Label the Graph
xlabel('Time (seconds)'); % Labels x-axis
ylabel('Distance (meters)'); % Labels y-axis
title('Time vs. Distance'); % Titles the graph
% Find min/max value of y and time when it occurs
[ymax, t1] = max(yVals);
[ymin, t2] = min(yVals);
% Print Part a
fprintf('The max value of y is %.2f meters and occurs at t=%.1f seconds \n', ymax, tVals(t1));
fprintf('The min value of y is %.2f meters and occurs at t=%.1f seconds \n', ymin, tVals(t2));
The max value of y is 14.00 meters and occurs at t=-5.0 seconds
The min value of y is 1.75 meters and occurs at t=-1.5 seconds
  댓글 수: 2
Mark Dillon
Mark Dillon 2015년 7월 22일
Thank you! That helps a lot.
Is it possible to turn that into a loop?
Star Strider
Star Strider 2015년 7월 22일
My pleasure!
You could turn it into a loop, but that would produce slower and less efficient code. One of MATLAB’s strengths is the vectorisation ability I used here, specifically to avoid a loop.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by