Plotting 1d Wave Equation

조회 수: 26 (최근 30일)
Kevin Lee
Kevin Lee 2016년 10월 16일
편집: John D'Errico 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