You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现带中心凹陷的类山形曲面z(x,y)的MATLAB建模?

Solution for Creating a Centrally Depressed Circular Surface in MATLAB

Hey there! Let's fix up your code to get that centrally depressed surface you're after—where the value hits 0 at the center and edge, peaks at R/2, and stays smooth with no abrupt changes.

First, let's clean up your original code: nested loops are unnecessary here, and we can calculate the radial distance r directly using meshgrid for efficiency.

Key Requirements Recap

We need a function z(r) (where r = sqrt(x² + y²) is the distance from the center) that satisfies:

  • z(0) = 0 (center is depressed to 0)
  • z(R/2) = maximum value (peak at half the radius)
  • z(R) = 0 (edge is 0)
  • Smooth, no abrupt changes (continuous first/second derivatives across the entire circle)

Option 1: Smooth Polynomial Function

A quartic (4th-degree) polynomial works perfectly here—it's smooth, symmetric, and meets all our constraints. The formula is:

z = 16 * (r.^2) .* ((R - r).^2) ./ (R^4);
  • At r=0 and r=R, z=0
  • At r=R/2, z=1 (you can scale this by multiplying by a constant if you want a larger peak)
  • First and second derivatives are continuous everywhere, so no sharp edges or abrupt changes.

Option 2: Trigonometric Function

If you prefer a trig-based approach, a squared sine function also fits the bill:

z = sin(pi * r / R).^2;
  • This is also smooth, with z=0 at r=0 and r=R, and z=1 at r=R/2
  • The shape is slightly different than the polynomial (more "rounded" near the peak), but still meets all your requirements.

Modified Full MATLAB Code

Here's how to implement either option (we'll use the polynomial as an example, but you can swap in the trig function easily):

R = 5;
theta = -pi:pi/180:pi;
x = R * cos(theta);
y = R * sin(theta);

% Create meshgrid for x and y
[u, v] = meshgrid(x, y);

% Calculate radial distance from center
r = sqrt(u.^2 + v.^2);

% Option 1: Quartic polynomial
z = 16 * (r.^2) .* ((R - r).^2) ./ (R^4);

% Uncomment below for Option 2: Trigonometric function
% z = sin(pi * r / R).^2;

% Plot the surface
mesh(u, v, z);
grid on;
xlabel('X');
ylabel('Y');
zlabel('Z');
title('Centrally Depressed Circular Surface');

Why This Works

  • We eliminate the nested loops by vectorizing calculations with meshgrid, which is way faster and more readable in MATLAB.
  • Both functions are symmetric around r=R/2, smooth, and exactly hit your required values at the center, peak, and edge.

内容的提问来源于stack exchange,提问作者Serge S

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:53:28