Filter löschen
Filter löschen

Problem with fplot - doesnt plot anything but when plotting the data individually it works

2 Ansichten (letzte 30 Tage)
Using this code, the aim is to calcuate the magnetic field produced by a solenoid within a 3D space. However, the fplot isnt working with my function, but when I substitute values into the fucntion, I get data points that make sense, and this is plotted using the for loop. Is anyone able to help fix this problem? As its an analytical solution, a function must be used to describe it. Thanks
The Code:
magnet.length = 0.25;
magnet.radius = 0.03;
magnet.position = [0, 0, 0];
magnet.I = 2.50E+01;
sphere.R = 0.25;
magnet.half_length = magnet.length / 2;
mu0 = 4*pi*1e-7;
syms x x1 % constants
f_magnet(x) = (mu0 * magnet.I * (magnet.radius^2)) / (2 * ((x-x1-magnet.position(1))^2 + magnet.radius^2)^(3/2)) * 1 / magnet.length; % Biot-Savart law
B_x_magnet(x) = int(f_magnet(x), x1, -magnet.half_length, magnet.half_length); % magnitude of B within the coil (uniform)
figure()
fplot(B_x_magnet(x), [-3*sphere.R 3*sphere.R])
hold off
figure()
for x = -3*sphere.R : 0.01 : 3*sphere.R
plot(x, B_x_magnet(x), 'o')
hold on
end
When plotting data using for loop:
When plotting using fplot:
  2 Kommentare
Dyuman Joshi
Dyuman Joshi am 18 Dez. 2023
There are many undefined parameters in the code, so we can not run your code to reproduce the result you got.
Matt
Matt am 18 Dez. 2023
Bearbeitet: Matt am 18 Dez. 2023
Hello, thanks for your reply, yes I have just realised that magnet.half_length and mu0 isnt included in the parameters.
magnet.half_length = magnet.length / 2;
mu0 = 4*pi*1e-7;
Thanks

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Dyuman Joshi
Dyuman Joshi am 18 Dez. 2023
You will have to define x as a real symbolic variable (or assume that the symbolic variable 'x' is a real number).
magnet.length = 0.25;
magnet.radius = 0.03;
magnet.position = [0, 0, 0];
magnet.I = 2.50E+01;
sphere.R = 0.25;
magnet.half_length = magnet.length / 2;
%% not defined, so value is assumed
mu0 = 1;
%% Define x as a real symbolic variable
syms x real
syms x1 % constants
f_magnet(x) = (mu0 * magnet.I * (magnet.radius^2)) / (2 * ((x-x1-magnet.position(1))^2 + magnet.radius^2)^(3/2)) * 1 / magnet.length % Biot-Savart law
f_magnet(x) = 
B_x_magnet(x) = int(f_magnet(x), x1, -magnet.half_length, magnet.half_length) % magnitude of B within the coil (uniform)
B_x_magnet(x) = 
figure()
fplot(B_x_magnet(x), [-3*sphere.R 3*sphere.R])
figure()
for x = -3*sphere.R : 0.01 : 3*sphere.R
plot(x, B_x_magnet(x), 'o')
hold on
end
  3 Kommentare

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by