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

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)

You probably need something like
e = hw3question1a(x)
as your function has to act upon something passed in.

9 Comments

Hmm, I am getting an empty graph. Is it possible its not taking those values from the other function still? Updated code:
function[err]= errorcalc(e,x)
t=0:1;
h=.02;
y=1;
x=Actualplot(t);
e=homework3question1a(t,y,h);
err=abs(x-e)/x;
semilogy(t,err);
xlabel('t')
ylabel('Error')
title('Error using Euler Method');
Here's the actualplot function which works:
function[y]= actualplot(t,y)
t=0:.02:1;
y=((5*exp(-(2*t)))-(3*exp(-(4*t))))/2;
plot(t,y)
and the homework3question1a function (euler's method):
function[y]= hw3question1a(t,y,h,f)
y=1;
h=.02;
t=0;
f=@(t,y)(3*exp(-(4*t)))-2*y;
for i=1:50
t(i+1)=t(i)+h;
y(i+1)=y(i)+h*f(t(i),y(i));
plot(t,y,'r--')
xlabel('t'); ylabel('y')
title('Eulers Method for function question1')
end
Would it make more sense to make a table of errors instead?
Your hw3question1a is defined with 4 variables but you are only passing in 3, and you are assigning to that 4th variable inside the routine. That function should be passed into the routine.
You should be holding off on the plot() until after your "for" loop.
After you do the plot in your hw3question1a, you immediately replace it with your semilogy() plot.
Your line
err=abs(x-e)/x;
needs to be
err = abs(x-e) ./ x;
I really appreciate the help man! So i figured out how to make the code a little more concise. Also a graph of the error doesn't make sense since they are small values so I made it into a table. The only problem now is the printf is giving me very weird numbers and not even displaying "i" in integers (it was supposed to be from 0 to 2500):
function[y]= hw3question1a(t,y,h,f)
y=1;
h=.02;
t=0;
f=@(t,y)(3*exp(-(4*t)))-2*y;
fprintf('Step 0: t= %12.8f, y %12.8f\n',t,y)
for i=1:2500
t(i+1)=t(i)+h;
y(i+1)=y(i)+h*f(t(i),y(i));
z=((5*exp(-(2*t)))-(3*exp(-(4*t))))/2;
xlabel('t'); ylabel('y')
title('Eulers Method for Question1a')
end
plot(t,y,'r--',t,z,'b')
%error%
errabs=abs(z-y)./z;
errrel=abs(z-y);
fprintf('Step %d: t= %6.4f, y= %18.15f,z= %18f, errabs=%18f, errrel=%18f\n',i,t,y,z,errabs,errrel)
valtable = [i(:),t(:),y(:),z(:),errabs(:),errrel(:)];
fprintf( 'Step %d: t= %6.4f, y= %18.15f,z= %18f, errabs=%18f, errrel=%18f\n', valtable .');
The .' is needed to get the outputs by rows. It is a feature of fprintf() and sprintf() that is confusing because people often think in rows, but when you understand how MATLAB works column by column the need for the .' does make sense.
Hmm now its giving this as a response: "Dimensions of matrices being concatenated are not consistent."
You should not be computing all of z each iteration of the loop
z=((5*exp(-(2*t)))-(3*exp(-(4*t))))/2;
should be
z(i+1) = ((5*exp(-(2*t(i+1))))-(3*exp(-(4*t(i+1)))))/2;
and make sure that you initialize z(1) as appropriate from t(1), which works out as
z = 1;
to start.
You are getting the error in concatenation because after the end of the loop, i has the single value 2500, which is the last value that was assigned to it. Use
i = 1 : length(t);
before the assignment to valtable
It's still giving the exact same error message with the following code:
function[y]= hw3question1a(t,y,h,f)
y=1;
h=.02;
t=0:h:50;
f=@(t,y)(3*exp(-(4*t)))-2*y;
fprintf('Step 0: t= %12.8f, y %12.8f\n',0,1)
z=1;
for i = 1 : length(t);
t(i+1)=t(i)+h;
y(i+1)=y(i)+h*f(t(i),y(i));
z(i+1) = ((5*exp(-(2*t(i+1))))-(3*exp(-(4*t(i+1)))))/2;
end
plot(t,y,'r--',t,z,'b')
xlabel('t'); ylabel('y')
title('Eulers Method for Question1a')
%error%
errabs=abs(z-y)./z;
errrel=abs(z-y);
valtable = [i(:),t(:),y(:),z(:),errabs(:),errrel(:)];
fprintf( 'Step %d: t= %6.4f, y= %18.15f,z= %18f, errabs=%18f, errrel=%18f\n', valtable .');
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.
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 Programming in Help Center and File Exchange

Asked:

on 7 Feb 2016

Commented:

on 26 Mar 2016

Community Treasure Hunt

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

Start Hunting!