Requesting help for piecewise ploting

clc
clear
syms r I % I = current, r = radius
a = 2;
b = 5;
c = 7;
B = piecewise(r<=0 & r>=a,((r/a)^2)*I, r>=a & r<=b, I, r>=b & r<=c, (c^2-r^2)/(c^2-b^2), r>c, 0);
fplot(B)
ax = gca;
ax.XAxisLocation = 'origin';
ax.YAxisLocation = 'origin';
grid on
title('\color[rgb]{0,0.5,0.5} Magentic Flux Density Magnitude')
xlabel('\sl \color[rgb]{0,0.5,0.5} r Radial Distance (cm)')
ylabel('\sl \color[rgb]{0,0.5,0.5} H Magnetic field magnitude (A/cm)')
Above is a what I would like to plot, but I recieve errors plotting. Below are my errors, I would like some guidance on how to correct my errors.
piecewise(2 <= r & r <= 5, I, 5 <= r & r <= 7, sym(49/24) - r^2/24, 7 < r, 0)
Error using fplot>singleFplot (line 240)
Input must be a function or functions of a single variable.
Error in fplot>@(f)singleFplot(cax,{f},limits,extraOpts,args) (line 200)
hObj = cellfun(@(f) singleFplot(cax,{f},limits,extraOpts,args),fn{1},'UniformOutput',false);
Error in fplot>vectorizeFplot (line 200)
hObj = cellfun(@(f) singleFplot(cax,{f},limits,extraOpts,args),fn{1},'UniformOutput',false);
Error in fplot (line 166)
hObj = vectorizeFplot(cax,fn,limits,extraOpts,args);
Thanks in advance!

답변 (1개)

Star Strider
Star Strider 2020년 10월 25일

0 개 추천

The Symbolic Math Toolbox and here particularly the piecewise funciton may not be the best option for this problem.
Use ordinary MATLAB plotting fucntions and code the piecewise expression and plot it as demonstrated here (not your function):
f = @(x) (2-2*x).*(x <= 0) + (2+2*x).*(x>0);
t = linspace(-3, 3);
figure
plot(t, f(t))
grid
ylim([0 max(ylim)])
Your function has two variables, ‘r’ and ‘I’, so it would have to be coded as:
B = @(r,I) (r<=0 & r>=a) .*(((r/a)^2)*I) + (r>=a & r<=b).*I + (I).*((c^2-r^2)/(c^2-b^2)) + (r>c).*(0);
both of variables would be generated fro their appropriate vectors with with ndgrid or meshgrid., then plotted with a 2D or 3D plotting function, depending on the result you want.

댓글 수: 5

Here is my code.
clc
clear
a = 2;
b = 5;
c = 7;
B = @(r,i) (r<=0 & r>=a) .*(((r/a).^2)*i) + (r>=a & r<=B).*i + (i).*((c^2-r^2)/(c^2-B^2)) + (r>c).*(0);
t = linspace(2, 7);
figure
plot(t, B(t))
grid
ylim([0 max(ylim)])
ax = gca;
ax.XAxisLocation = 'origin';
ax.YAxisLocation = 'origin';
grid on
title('\color[rgb]{0,0.5,0.5} Magentic Flux Density Magnitude')
xlabel('\sl \color[rgb]{0,0.5,0.5} r Radial Distance (cm)')
ylabel('\sl \color[rgb]{0,0.5,0.5} H Magnetic field magnitude (A/cm)')
error is below.
Not enough input arguments.
Error in untitled4 (line 6)
B = @(r,i) (r<=0 & r>=a) .*(((r/a).^2)*i) + (r>=a & r<=B).*i + (i).*((c^2-r^2)/(c^2-B^2)) + (r>c).*(0);
Walter Roberson
Walter Roberson 2020년 10월 25일
Your B is defined in terms of current and radius. You are trying to invoke B in terms of just t.
What is the formula for varying current with respect to time?
What is the formula for varying radius with respect to time?
Perhaps you should be using fsurf() with current and radius parameters and no time parameter?
Star Strider
Star Strider 2020년 10월 25일
Walter — Thank you!
Jonathan Hawkins
Jonathan Hawkins 2020년 10월 26일
My apologies for the lack of a better explanation, I was trying to use r as my x-axis while trying to magnetic flux density for all regions while plotting the abs(B) vs. r. I chose arbitrary values of a=2, b=5, c=7. My result is B={(a(phi)*((u0*I*r)/2pi*a^2),0<=r<=a) ; (a(phi)*((u0*I*r)/2pi*r),a<r<b); (a(phi)*((u0*I*r)/2pi*r)*((c^2-r^2)/(c^2-b^2)),b<=r<=c); (0, r>c)}
I greatly appreciate your time.
Star Strider
Star Strider 2020년 10월 26일
Our pleasure!
If my Answer helped you solve your problem, please Accept it!
.

댓글을 달려면 로그인하십시오.

카테고리

제품

릴리스

R2020b

태그

질문:

2020년 10월 24일

댓글:

2020년 10월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by