有人可以解释如何使用Contourf在MATLAB中绘制该总和吗? [英] Can someone explain how to graph this sum in MATLAB using contourf?

查看:90
本文介绍了有人可以解释如何使用Contourf在MATLAB中绘制该总和吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我要说的是,这是家庭作业(我关于stackoverflow的第一个家庭作业问题!).但是我不希望您为我解决它,我只需要一些指导!

相关方程式如下:

告诉我取N = 50,phi1 = 300,phi2 = 400、0 <= x <= 1和0 <= y <= 1,并让x和y为100个等距向量点,包括终点.

所以我要做的第一件事就是设置这些变量,并使用x = linspace(0,1)和y = linspace(0,1)来制作正确的向量.

问题是编写一个名为potential.m的MATLAB脚本文件,该文件将使用内置函数 contourf 计算phi(x,y)并针对x和y绘制填充轮廓图(请参见MATLAB中的help命令作为示例).确保正确标记了该图. (提示:您的域的顶部和底部应该比大约300度的左侧和右侧的温度高400度左右.)

但是,以前,我已经使用x或y作为常数来计算phi.在两个都是变量的情况下,我应该如何计算呢?在遍历y向量的每个数字时,将x赋值给矩阵,一次又一次遍历y的每个值后,将x递增到向量中的下一个数字时,我是否保持x稳定?然后执行相同的过程,但要逐渐增加y?

如果是这样,我一直在使用一个循环,它每次循环遍历所有100个值时都会递增到下一行.如果这样做的话,最终将得到一个包含200行100列的大型矩阵.我将如何在linspace函数中使用它?

如果是正确的话,这就是我寻找矩阵的方式:

clear
clc
format compact
x = linspace(0,1);
y = linspace(0,1);
N = 50;
phi1 = 300;
phi2 = 400;
phi = 0;
sum = 0;
for j = 1:100
    for i = 1:100
        for n = 1:N
            sum = sum + ((2/(n*pi))*(((phi2-phi1)*(cos(n*pi)-1))/((exp(n*pi))-(exp(-n*pi))))*((1-(exp(-n*pi)))*(exp(n*pi*y(i)))+((exp(n*pi))-1)*(exp(-n*pi*y(i))))*sin(n*pi*x(j)));
        end
        phi(j,i) = phi1 - sum;
    end
end
for j = 1:100
    for i = 1:100
        for n = 1:N
            sum = sum + ((2/(n*pi))*(((phi2-phi1)*(cos(n*pi)-1))/((exp(n*pi))-(exp(-n*pi))))*((1-(exp(-n*pi)))*(exp(n*pi*y(j)))+((exp(n*pi))-1)*(exp(-n*pi*y(j))))*sin(n*pi*x(i)));
        end
        phi(j+100,i) = phi1 - sum;
    end
end

这是Contourf的定义.我想我必须使用Contourf(X,Y,Z):

contourf(X,Y,Z),contourf(X,Y,Z,n)和contourf(X,Y,Z,v)使用X和Y绘制Z的填充轮廓图以确定x和y轴限制.当X和Y是矩阵时,它们的大小必须与Z相同,并且必须单调递增.

这是新代码:

N = 50;
phi1 = 300;
phi2 = 400;
[x, y, n] = meshgrid(linspace(0,1),linspace(0,1),1:N)
f = phi1-((2./(n.*pi)).*(((phi2-phi1).*(cos(n.*pi)-1))./((exp(n.*pi))-(exp(-n.*pi)))).*((1-(exp(-1.*n.*pi))).*(exp(n.*pi.*y))+((exp(n.*pi))-1).*(exp(-1.*n.*pi.*y))).*sin(n.*pi.*x));
g = sum(f,3);
[x1,y1] = meshgrid(linspace(0,1),linspace(0,1));
contourf(x1,y1,g)

解决方案

矢量化代码.例如,您可以使用以下命令编写f(x,y,n):

 [x y n] = meshgrid(-1:0.1:1,-1:0.1:1,1:10);
 f=exp(x.^2-y.^2).*n ;

f是3D矩阵,现在恰好在正确尺寸上sum ...

 g=sum(f,3);

为了使用contourf,我们将仅采用x,y的2D部分:

 [x1 y1] = meshgrid(-1:0.1:1,-1:0.1:1);    
 contourf(x1,y1,g)

I'm going to start off by stating that, yes, this is homework (my first homework question on stackoverflow!). But I don't want you to solve it for me, I just want some guidance!

The equation in question is this:

I'm told to take N = 50, phi1 = 300, phi2 = 400, 0<=x<=1, and 0<=y<=1, and to let x and y be vectors of 100 equally spaced points, including the end points.

So the first thing I did was set those variables, and used x = linspace(0,1) and y = linspace(0,1) to make the correct vectors.

The question is Write a MATLAB script file called potential.m which calculates phi(x,y) and makes a filled contour plot versus x and y using the built-in function contourf (see the help command in MATLAB for examples). Make sure the figure is labeled properly. (Hint: the top and bottom portions of your domain should be hotter at about 400 degrees versus the left and right sides which should be at 300 degrees).

However, previously, I've calculated phi using either x or y as a constant. How am I supposed to calculate it where both are variables? Do I hold x steady, while running through every number in the vector of y, assigning that to a matrix, incrementing x to the next number in its vector after running through every value of y again and again? And then doing the same process, but slowly incrementing y instead?

If so, I've been using a loop that increments to the next row every time it loops through all 100 values. If I did it that way, I would end up with a massive matrix that has 200 rows and 100 columns. How would I use that in the linspace function?

If that's correct, this is how I'm finding my matrix:

clear
clc
format compact
x = linspace(0,1);
y = linspace(0,1);
N = 50;
phi1 = 300;
phi2 = 400;
phi = 0;
sum = 0;
for j = 1:100
    for i = 1:100
        for n = 1:N
            sum = sum + ((2/(n*pi))*(((phi2-phi1)*(cos(n*pi)-1))/((exp(n*pi))-(exp(-n*pi))))*((1-(exp(-n*pi)))*(exp(n*pi*y(i)))+((exp(n*pi))-1)*(exp(-n*pi*y(i))))*sin(n*pi*x(j)));
        end
        phi(j,i) = phi1 - sum;
    end
end
for j = 1:100
    for i = 1:100
        for n = 1:N
            sum = sum + ((2/(n*pi))*(((phi2-phi1)*(cos(n*pi)-1))/((exp(n*pi))-(exp(-n*pi))))*((1-(exp(-n*pi)))*(exp(n*pi*y(j)))+((exp(n*pi))-1)*(exp(-n*pi*y(j))))*sin(n*pi*x(i)));
        end
        phi(j+100,i) = phi1 - sum;
    end
end

This is the definition of contourf. I think I have to use contourf(X,Y,Z):

contourf(X,Y,Z), contourf(X,Y,Z,n), and contourf(X,Y,Z,v) draw filled contour plots of Z using X and Y to determine the x- and y-axis limits. When X and Y are matrices, they must be the same size as Z and must be monotonically increasing.

Here is the new code:

N = 50;
phi1 = 300;
phi2 = 400;
[x, y, n] = meshgrid(linspace(0,1),linspace(0,1),1:N)
f = phi1-((2./(n.*pi)).*(((phi2-phi1).*(cos(n.*pi)-1))./((exp(n.*pi))-(exp(-n.*pi)))).*((1-(exp(-1.*n.*pi))).*(exp(n.*pi.*y))+((exp(n.*pi))-1).*(exp(-1.*n.*pi.*y))).*sin(n.*pi.*x));
g = sum(f,3);
[x1,y1] = meshgrid(linspace(0,1),linspace(0,1));
contourf(x1,y1,g)

解决方案

Vectorize the code. For example you can write f(x,y,n) with:

 [x y n] = meshgrid(-1:0.1:1,-1:0.1:1,1:10);
 f=exp(x.^2-y.^2).*n ;

f is a 3D matrix now just sum over the right dimension...

 g=sum(f,3);

in order to use contourf, we'll take only the 2D part of x,y:

 [x1 y1] = meshgrid(-1:0.1:1,-1:0.1:1);    
 contourf(x1,y1,g)

这篇关于有人可以解释如何使用Contourf在MATLAB中绘制该总和吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆