Plotting 1d Wave Equation

26 次查看(过去 30 天)
Kevin Lee
Kevin Lee 2016-10-16
Hi guys,
I've solved a 1D Wave Equation as required and am trying to plot it to no success.
My code at the moment is this:
clear all;
close all;
clc;
x = linspace(0,2);
n = 2;
t = input('Time: ');
Cn = (32*(((sin(((n-2)*pi)/2)))/(n-2)-((sin(((n+2)*pi)/2)))/(n+2))/pi) + ((exp(2))*(((sin(((n-6)*pi)/2)))/(n-6)-((sin(((n+6)*pi)/2)))/(n+6))/pi) + (25*(((sin(((n-12)*pi)/2)))/(n-12)-((sin(((n+12)*pi)/2)))/(n+12))/pi);
Dn = ((6*(((sin(((n-4)*pi)/2)))/(n-4)-((sin(((n+4)*pi)/2)))/(n+4))/(5*n*pi^2)) - (16*(((sin(((n-5)*pi)/2)))/(n-5)-((sin(((n+5)*pi)/2)))/(n+5))/(5*n*pi^2)));
u = (sin((n*pi*x)/2))*(Cn*cos(5*n*pi*t)+Dn*sin(5*n*pi*t));
plot(x, u)
When running the script, the matrix for u comes up with NaN, and thus nothing is plotted. Could anyone please help me with this.
Thank you.
  1 个评论
John D'Errico
John D'Errico 2016-10-16
Do people need to guess what value for Time you input when you run this? The crystal ball is so foggy, I imagine that most people will not know the value you used, and it may be pertinent.

请先登录,再进行评论。

回答(2 个)

Jakub Mrowka
Jakub Mrowka 2016-10-16
Hi, I analysed your code and here is solution NaN is when 0/0, and I found that part in your code:
Cn = (32*( ((sin(((n-2)*pi)/2)))/(n-2)-((sin(((n+2)*pi)/2)))/(n+2))/pi) + ((exp(2))*(((sin(((n-6)*pi)/2)))/(n-6)-((sin(((n+6)*pi)/2)))/(n+6))/pi) + (25*(((sin(((n-12)*pi)/2)))/(n-12)-((sin(((n+12)*pi)/2)))/(n+12))/pi);
as you remember you have n=2; so the first part is like sin(0*something)/0 whcih means whole is 0/0, which is NaN. And if you add something to NaN it's still NaN. just change n to other number than 2,4,5,6,12

John D'Errico
John D'Errico 2016-10-16
编辑:John D'Errico 2016-10-16
In general, it helps if you tell ALL of the pertinent parameters in your problems. Otherwise, it can be difficult to know what you used.
Here, if you just bother to look at your first expression, think about what it is doing.
n = 2;
((sin(((n-2)*pi)/2)))/(n-2)
ans =
NaN
What is 0/0 anyway? NaN.
Perhaps you need to do something special for that term. Perhaps you hoped that 0/0 might be interpreted as 1, or some other number. It is not so. In fact, it has no valid mathematical value at all, which is why NaN is returned. The fact is, 0/0 can be argued to have any value you want it to have, anything from -inf to inf, 0, NaN, 1, pi/2, take your pick from infinitely many possible values.

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by