Clear Filters
Clear Filters

How to handle Error updating FunctionLine?

20 views (last 30 days)
Niloufar
Niloufar on 28 Dec 2022
Commented: Steven Lord on 28 Dec 2022
I want to make the gif of a wave equation that the answer is:
and the initial conditions are:
Here is my code and I don't understand and I don't know how to fix it.
clear;clc;close all;
a = 1;
t0 = 1;
alpha = 1;
syms x;
%define lambda function
f = @(x) ((-a/t0).*x-a)*(x>-2*t0-0.1 & x<-t0) + ((a/t0).*x+a)*(x>-t0 & x<=0) + ((-a/t0).*x+a)*(x>0 & x<t0) + ((a/t0).*x-a)*(x>t0 & x<2*t0+0.1);
g = @(t) alpha*((-a/t0).*t-a)*(t>-2*t0-0.1 & t<-t0) + ((a/t0).*t+a)*(t>-t0 & t<=0) + ((-a/t0).*t+a)*(t>0 & t<t0) + ((a/t0).*t-a)*(t>t0 & t<2*t0+0.1);
for c = 0:0.05:1
desired_fun = @(x) (1/(2*c)).*integral(g,x-c,x+c);
my_plot = fplot((@(x) ((f(x-c) + f(x+c))/2) + desired_fun),[-t0 t0]);
frame = getframe(1);
im = frame2im(frame); %pause(0.01);
[imind,cm] = rgb2ind(im,256);
imwrite(imind,cm,"C:\Users\ernika\wave_equation.gif",'gif','WriteMode','append'); %saved as mygif1
drawnow;
if(c~=1)
delete(my_plot)
end
end

Answers (1)

Walter Roberson
Walter Roberson on 28 Dec 2022
You define an anonymous function of one variable. In the next line you define a second anonymous function and ask to fplot it. When invoked, the second anonymous function does a calculation and tries to add the first anonymous function to the result.
This fails because it is not permitted to do arithmetic on anonymous function handles.
Remember that the only operations permitted on anonymous functions are:
  • displaying the anonymous function
  • calling functions() to get some internal information about the anonymous function
  • asking for the formula associated with the function (as text)
  • asking for a text representation of the anonymous function
  • storing the handle into a variable except one indexed using ()
  • obscure exception: storing the handle to a non-existent variable indexed with (1)
  • passing the handle as a parameter
  • or invoking the anonymous function, which always requires () after the handle unless you use feval()
In no situation can you do something like
f = @(x)x*2
f + 5
as that is not one of the listed operations.
In your second anonymous function you are not invoking the first function: invoking would require passing a parameter to the first function.
  1 Comment
Steven Lord
Steven Lord on 28 Dec 2022
In no situation can you do something like
f = @(x)x*2
f + 5
What you can do instead is to add the values obtained by evaluating the anonymous function and another number.
f = @(x) 2*x;
g = @(x) f(x) + 5; % Evaluate f at the input argument and add 5 to the result
g(4) % 2*4 + 5 = 13
ans = 13

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!