How to handle mupadmex error?

2 次查看(过去 30 天)
Niloufar
Niloufar 2022-12-27
I have a wave equation that is like this:
and here is the initial conditions:
I should create the gif for a variety of c but I get mupadmex error Which I don't know how to fix and here is my code:
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 = @(x) alpha.*f(x);
for c = 0:0.05:1
h(x) = int(g(x));
G = matlabFunction(h);
desired_fun = G(x+c) - G(x-c);
my_plot = fplot(((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

回答(2 个)

Ajay Gajulapally
Ajay Gajulapally 2023-1-2
The mupadmex error is caused because of your initial values of a, t0, alpha.
Consider Changing those values which resolves your mupadmex error.
a = 10;
t0 = 100;
alpha = 1000;

Walter Roberson
Walter Roberson 2023-1-2
Do not define the functions that way. The symbolic toolbox does not define a result for multiplying a symbolic expression by a logical value.
You need to define the function symbolically using piecewise. Then integrate it with a definite integral specifying the variable of integration and the upper and lower bound. You can make a bound the variable of integration if you need to.
Then use matlabFunction with the 'file' option and with 'optimize' off. You must use 'file' for this purpose. 'file' option is able to generate code for piecewise.
Now, the function that results is not vectorized, so if you try to use it in a context such as integral() or fplot() then that will fail; you would need an arrayfun wrapper in such cases.
You appear to be passing symbolic x back into the result of matlabFunction, but that is going to fail.
I would suggest that you reconsider using matlabFunction and use
G = h;
which will avoid the issues about code generation and will allow a proper desired_function to be defined symbolically.

标签

Community Treasure Hunt

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

Start Hunting!

Translated by