How do you call a function from another m.file?
Show older comments
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
on 7 Feb 2016
You probably need something like
e = hw3question1a(x)
as your function has to act upon something passed in.
9 Comments
Mohannad Abboushi
on 7 Feb 2016
Edited: Mohannad Abboushi
on 7 Feb 2016
Walter Roberson
on 7 Feb 2016
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;
Mohannad Abboushi
on 7 Feb 2016
Edited: Mohannad Abboushi
on 7 Feb 2016
Walter Roberson
on 7 Feb 2016
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.
Mohannad Abboushi
on 7 Feb 2016
Walter Roberson
on 7 Feb 2016
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
Mohannad Abboushi
on 8 Feb 2016
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
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')
Categories
Find more on Programming 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!