问题描述
我一直在尝试执行以下分段功能:
I've been trying to do the following piecewise function:
y(x,t) = { 0 (t - 5) < 0
{ (t - 5)*(t - x) x < (t - 5)
{ (t + x^2) x >= (t - 5)
由于x = 0:.5:10和t = 0:.1:10,我似乎不了解如何绘制此函数的图形.我知道如何在没有t的情况下执行此操作,但是当包含t且与x相比间隔不同时,我会迷路.
I don't seem to understand how to graph this function since x = 0:.5:10 and t = 0:.1:10. I know how to do this without the t but I get lost when the t is included and has different intervals compared to the x.
谢谢,Y_Y
推荐答案
乘以真实值通常是有效的,然后您只需将这些值加在一起即可.
Multiplying by the truth value usually works, and then you simply add the values together:
y = @(x,t)( (t-5).*(t-x).*(x<(t-5)) + (t+x.^2).*(x>=(t-5)) );
由于我的笔记本电脑上没有MATLAB,我现在无法测试,但是它可以按照您希望的方式工作.
I can't test this right now as I don't have MATLAB on my laptop, but it should work the way you want it to.
您的实际问题似乎与x
和t
具有不同尺寸的事实有关.可以使用 meshgrid
函数对其进行修复".用于多维函数的两个矩阵:
Your real problem seems to be related to the fact that x
and t
are of different dimensions. This can be "fixed" using the meshgrid
function, which creates two matrices for use in multi-dimensional functions:
[X, T] = meshgrid([1:.5:10], [0:.1:10]);
Z = y(X, T); % With y(x,t) as above
Z
现在是一个矩阵,其中包含相应的x
和t
的y
值.可以使用surf
将其绘制为表面:
Z
is now a matrix containing the values of y
for different x
and t
accordingly. This can be plotted as a surface using surf
:
surf(X, T, Z);
这篇关于在Matlab中如何执行具有多个变量的分段方程式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!