Doing Numerics in Matlab

  • Thread starter skrieger
  • Start date
  • Tags
    Matlab
In summary: Expert SummarizerIn summary, a forum user is seeking help with programming a numerical solution for a simple hyperbolic equation in a course on PDEs. They have a strong understanding of the theory but need guidance with coding. They have made progress in setting up a basic difference scheme, but need help with implementing it in Matlab. The expert suggests using a loop to iterate through time steps and updating the variable v using the derivative operator. They also recommend seeking additional resources and assistance as needed.
  • #1
skrieger
6
0
1. Homework Statement

This is for a course in numerical solutions of PDEs. So far it has all been theory of PDEs which is great since I am the worst programmer in the world. Right now I have to program the following: to solve the simplest hyperbolic equation,

du/dt = du/dx,

using the scheme

v(_j)(^n+1) = (1 + kD_0)v(_j)(^n)
v(_j)(^0) = sin(x_j)

for h(x-mesh size) = (2*pi)/(N+1), k(t-mesh size) = 2h/3, total time T = [0, 2*pi], in the three cases N = 20, 40 and 80.

the idea being to examine the errors between this result and the analytic solution, which should increase exponentially.


2. The attempt at a solution

I prefer to learn this in Matlab, but my knowledge base does not extend very far at all beyond plugging things into ode45. I just need someone to help me figure out how to set up basic difference schemes, I don't get the code beyond setting up linspaces, etc. So far all I have is

N = 32;
M = 20;
t = linspace(0,(2*pi),N)';
dt = t(2) - t(1);
x = linspace(0, (2*pi),M)';
dx = x(2) - x(1);
% Create the first derivative operator
D1 = diag(ones(N,1));
D2 = diag(-ones(N,1),-1);
D2 = D2(1:N,1:N);
D = (D1+D2)/((2*pi)/21);

but even if this is the proper formation of the derivative operator I'm not sure how to set up the actual scheme from here. No need for anyone to solve for the three N's or talk about errors, I just need help learning how to put these schemes together.

Thanks!
 
Physics news on Phys.org
  • #2


Thank you for reaching out for help with your numerical solutions of PDEs course. It's great to hear that you have a strong understanding of the theory of PDEs, but may need some assistance with programming. I understand the importance of being able to effectively code in order to solve complex problems.

Based on the information you provided, it seems like you are on the right track with setting up the basic difference scheme. Your code for creating the first derivative operator looks correct, but I would suggest double checking to make sure it is accurately representing the equation you are trying to solve.

To set up the actual scheme, you will need to use a loop to iterate through the time steps and use the derivative operator to update the values of your variable v at each spatial point. This can be done by using the formula you provided in your post: v(_j)(^n+1) = (1 + kD_0)v(_j)(^n). You will also need to specify the initial conditions for v(_j)(^0) = sin(x_j).

I understand that you prefer to use Matlab for this problem, so I would suggest looking into the "for" loop function in Matlab to help you iterate through the time steps and update your variable v. Additionally, there are many online resources and tutorials available for coding difference schemes in Matlab that may be helpful to you.

I hope this helps guide you in the right direction. Don't be discouraged if you run into some challenges along the way, as programming can be a complex and iterative process. Just remember to break down the problem into smaller steps and don't hesitate to seek out additional resources or assistance from your instructor or classmates.

Best of luck with your project!
 

What is Matlab?

Matlab is a high-level programming language and interactive environment for numerical computation, visualization, and programming. It is commonly used in scientific and engineering fields for data analysis, modeling, and simulation.

What are the benefits of using Matlab for numerical computations?

Matlab offers a comprehensive set of built-in functions and tools for performing numerical computations, making it faster and more efficient than writing code from scratch. It also has a user-friendly interface and extensive documentation, making it easy to learn and use.

How do I import and manipulate data in Matlab?

Matlab has various functions for importing data from different file formats, such as CSV, Excel, and text files. Once imported, data can be manipulated using built-in functions or custom code. The data can also be visualized using Matlab's plotting and graphing tools.

Can Matlab handle large datasets?

Yes, Matlab has the capability to handle large datasets through its memory management and parallel computing features. It also supports distributed computing, allowing for even larger datasets to be processed.

What are the limitations of using Matlab for numerical computations?

While Matlab is a powerful tool for numerical computations, it may not be the best choice for certain applications. It is not suitable for real-time applications, and its license can be costly for some users. It also has limitations in handling complex data structures and algorithms compared to other programming languages.

Similar threads

  • Calculus and Beyond Homework Help
Replies
10
Views
967
  • Calculus and Beyond Homework Help
Replies
1
Views
181
  • Calculus and Beyond Homework Help
Replies
16
Views
547
  • Calculus and Beyond Homework Help
Replies
3
Views
397
  • Calculus and Beyond Homework Help
Replies
3
Views
551
  • Calculus and Beyond Homework Help
Replies
2
Views
898
  • Calculus and Beyond Homework Help
Replies
13
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
547
  • Calculus and Beyond Homework Help
Replies
1
Views
961
  • Calculus and Beyond Homework Help
Replies
24
Views
770
Back
Top