Dear there,
I have a piecewise function u(x,t) as following code. I want to plot u(x,t) on the region (x,t) ∈ [-1,1]x[-2,2] by using the contour surface plot as the link
Could you help me please?
Thanks
clc;
clear all;
syms x t
t1 = exp(x);
t21 = -5 .* t;
t3 = exp(t21);
t5 = exp((1 + t21));
t4 = ((0 <= t & t <= 1/2) .* 1.73205);
t5 = ((0 <= t & t <= 1/2) .* (30.9839 .* t - 7.74597));
t6 = ((1/2 <= t & t <= 1) .* 1.73205);
t7 = ((1/2 <= t & t <= 1) .* (30.9839 .* t - 23.2379));
t6 = (1 + t5).^2;
t2 = 1 ./ t6;
t7 = (1 + t3).^2;
t3 = 1 ./ t7;
t8 = -0.00399646;
t9 = 0.00922094;
t10 = 0.0415432;
t11 = 0.0603743;
t12 = 0.177671;
t13 = ((0 <= x & x <= 1/2) .* 1.73205);
t14 = ((0 <= x & x <= 1/2) .* (30.9839 .* x - 7.74597));
t15 = ((1/2 <= x & x <= 1) .* 1.73205);
t16 = ((1/2 <= x & x <= 1) .* (30.9839 .* x - 23.2379));
t8 = (1 + t1).^2;
t1 = 1 ./ t8;
u = -1/4 + (-0.00243052 .* t13 - 0.000809061 .* t14 - 0.00195593 .* t16 - 0.0152378 .* t15) .* t4 + (-0.00043359 .* t13 - 0.000146113 .* t14 - 0.000477063 .* t16 - 0.00319022 .* t15) .* t5 + (-0.00276115 .* t13 - 0.000933166 .* t14 - 0.00314361 .* t16 - 0.0207985 .* t15) .* t6 + t7 .* (0.000172747 .* t13 + 0.0013619 .* t15 + 0.00021141 .* t16 + 5.86775e-05 .* t14) + x .* (t10 .* t4 + t11 .* t6 + t5 .* t9 + t7 .* t8 + t12 + t2 - t3) + t3 + t1;

 Accepted Answer

create a meshgrid of x and t values, X and T. Then
Z = double(subs(u, {x, t}, {X, T})) ;
surfc(X, Y, Z)

5 Comments

student_md
student_md on 22 Oct 2020
Edited: student_md on 22 Oct 2020
I wrote a code as follows
x=0:0.1:1;
t=0:0.1:2;
[X,T]=meshgrid(x,t);
Z = double(subs(u, {x, t}, {X, T})) ;
surfc(X, T, Z);
But I get the following error: What is wrong?
Error in sym/subs (line 145)
G = mupadsubs(F,X,Y);
Error in piecewise3dplot (line 33)
Z = double(subs(u, {x, t}, {X, T})) ;
xvec = 0:0.1:1;
tvec = 0:0.1:2;
[X,T] = meshgrid(xvec, tvec);
Z = double(subs(u, {x, t}, {X, T})) ;
You were overwriting the symbolic nature of x and t with numeric variables, so they were no longer the names of symbolic variables when you got to the subs() statement.
student_md
student_md on 26 Oct 2020
Edited: student_md on 26 Oct 2020
xvec = 0:0.1:1;
tvec = 0:0.1:2;
[X,T] = meshgrid(xvec, tvec);
Z = double(subs(u, {x, t}, {X, T}))
But the code is not still working...When I run the your last code, I get the following errors: I found some solutions of the errors on this link. But I can' t solve and still have errors.
Error in sym/double (line 707)
Xstr = mupadmex('symobj::double', S.s, 0);
Error in piecewise3dplot (line 31)
Z = double(subs(u, {x, t}, {X, T}));
syms x t
t1 = exp(x);
t21 = -5 .* t;
t3 = exp(t21);
t5 = exp((1 + t21));
t4 = ((0 <= t & t <= 1/2) .* 1.73205);
t5 = ((0 <= t & t <= 1/2) .* (30.9839 .* t - 7.74597));
t6 = ((1/2 <= t & t <= 1) .* 1.73205);
t7 = ((1/2 <= t & t <= 1) .* (30.9839 .* t - 23.2379));
t6 = (1 + t5).^2;
t2 = 1 ./ t6;
t7 = (1 + t3).^2;
t3 = 1 ./ t7;
t8 = -0.00399646;
t9 = 0.00922094;
t10 = 0.0415432;
t11 = 0.0603743;
t12 = 0.177671;
t13 = ((0 <= x & x <= 1/2) .* 1.73205);
t14 = ((0 <= x & x <= 1/2) .* (30.9839 .* x - 7.74597));
t15 = ((1/2 <= x & x <= 1) .* 1.73205);
t16 = ((1/2 <= x & x <= 1) .* (30.9839 .* x - 23.2379));
t8 = (1 + t1).^2;
t1 = 1 ./ t8;
u = -1/4 + (-0.00243052 .* t13 - 0.000809061 .* t14 - 0.00195593 .* t16 - 0.0152378 .* t15) .* t4 + (-0.00043359 .* t13 - 0.000146113 .* t14 - 0.000477063 .* t16 - 0.00319022 .* t15) .* t5 + (-0.00276115 .* t13 - 0.000933166 .* t14 - 0.00314361 .* t16 - 0.0207985 .* t15) .* t6 + t7 .* (0.000172747 .* t13 + 0.0013619 .* t15 + 0.00021141 .* t16 + 5.86775e-05 .* t14) + x .* (t10 .* t4 + t11 .* t6 + t5 .* t9 + t7 .* t8 + t12 + t2 - t3) + t3 + t1;
U = matlabFunction(u);
xvec = 0:0.1:1;
tvec = 0:0.1:2;
[X,T] = meshgrid(xvec, tvec);
Z = U(X,T);
surf(X, T, Z)

Sign in to comment.

More Answers (0)

Categories

Find more on Mathematics in Help Center and File Exchange

Products

Release

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!