How do you call a function from another m.file?

2 views (last 30 days)
I am trying to use another function's values to make a plot of error for a mathematical method. Here's my code so far. "Actualplot" is the name of a function that has the actual plot of an equation while "hw3question1a"is a function that used Euler's method to graph an equation. I'm not sure how to call the functions so that I can get their values for the error.
function[err]= errorcalc(x,e)
x=actualplot;
e=hw3question1a
t=0:50;
err=abs(x-e)/x;
plot(t,err);
xlabel('t') % Labels ??x?? axis
ylabel('Error') % Labels ??y?? axis
title('Error using Euler Method');

Answers (1)

Walter Roberson
Walter Roberson on 7 Feb 2016
You probably need something like
e = hw3question1a(x)
as your function has to act upon something passed in.
  9 Comments
Walter Roberson
Walter Roberson on 8 Feb 2016
As I said,
"Use i = 1 : length(t); before the assignment to valtable."
I just did and the concatenation came out fine.
By the way, I suggest using a %g format for the outputs rather than a %f format, as the outputs get small but your error keeps growing.
Muhammad Fahad
Muhammad Fahad on 26 Mar 2016
Actually I want to solve the system of two coupled differential equations in MATLAB by using implicit Euler's Method.My teacher suggests me to use the command "fsolve".Here I am providing the MATLAB code that I construct.I know there must be a very stupid error at line 13 but anyways help me to solve this problem: clear all clc
f = @(y) [-80.6*y(1)+119.4*y(2); 79.6*y(1)-120.4*y(2)];
h=1/100;
y(:,1)=[1 ; 4]; t(1)=0;
for i =1:2
y(:,i+1)=fsolve(@(z) -z+y(:,i)+h*f(z),[-10 10])
t(i+1)=t(i)+h;
end
plot(t,y(1,:),'b',t,y(2,:),'b')
hold on
ts=0:0.001:1;
ys(1,:)=(3)*exp(-ts)+(-2)*exp(-200*ts);
ys(2,:)=(2)*exp(-ts)+(2)*exp(-200*ts);
plot(ts,ys(1,:),'r',ts,ys(2,:),'g')

Sign in to comment.

Categories

Find more on 2-D and 3-D Plots 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!