How to pass equation to a function where the function subplots graphs?
Show older comments
This is my main function:
t = 3;
y_exact_overdamped = @(x)((5/3)*exp(-x))-((2/3)*exp(-4*x));
y_title_overdamp = "Position vs Time, Overdamped";
figure_d(t, y_exact_overdamped, y_title_overdamp)
And this is the function code:
function figure_d(t, y_exact, title_y)
subplot(2,1,1);
x = linspace(0,t);
y1 = y_exact;
plot(x,y1);
title(title_y)
xlabel('time')
ylabel('position')
When I take out @(x) from the main function it shows an error that x is undefined. But when I use @(x) it is an invalid input when subplotting. I do not understand how I can pass a equation to a function and plot it using subplot as I will need two graphs in one figure. Help would be appreciated. Thanks.
1 Comment
Rik
on 10 Aug 2018
And if you use y1 = y_exact(x);?
Answers (1)
James Tursa
on 10 Aug 2018
y_exact_overdamped is a function handle. You pass that into your function which gets it as y_exact. You then assign this to y1. All of these are still just function handles. You need to evaluate them with your x values. E.g.,
plot(x,y1(x));
Categories
Find more on Subplots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!